Skip to content

Commit b9de3bb

Browse files
committed
Fixes for memory leaks in test.c with wc_AesNew and wc_HashNew.
1 parent dbd3484 commit b9de3bb

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

wolfcrypt/test/test.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6265,8 +6265,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
62656265
#endif
62666266

62676267
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
6268-
hash->isAllocated = 1; /* free manually */
6269-
(void)wc_HashFree(hash, WC_HASH_TYPE_NONE);
6268+
if (hash != NULL) {
6269+
hash->isAllocated = 1; /* free manually */
6270+
(void)wc_HashFree(hash, hash->type);
6271+
}
62706272
#endif
62716273

62726274
return 0;
@@ -15686,7 +15688,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
1568615688

1568715689
static wc_test_ret_t aesccm_256_test(void)
1568815690
{
15689-
wc_test_ret_t ret;
15691+
wc_test_ret_t ret = 0;
1569015692
/* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
1569115693
WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
1569215694
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
@@ -15708,15 +15710,14 @@ static wc_test_ret_t aesccm_256_test(void)
1570815710
byte atag[sizeof(exp_tag)];
1570915711

1571015712
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
15711-
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
15713+
Aes* aes = wc_AesNew(HEAP_HINT, devId);
1571215714
if (aes == NULL) {
15713-
return MEMORY_E;
15715+
ret = WC_TEST_RET_ENC_EC(MEMORY_E);
1571415716
}
1571515717
#else
1571615718
Aes aes[1];
15717-
#endif
15718-
1571915719
ret = wc_AesInit(aes, HEAP_HINT, devId);
15720+
#endif
1572015721
if (ret == 0) {
1572115722
ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
1572215723
}
@@ -15751,10 +15752,6 @@ static wc_test_ret_t aesccm_256_test(void)
1575115752

1575215753
wc_AesFree(aes);
1575315754

15754-
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
15755-
XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
15756-
#endif
15757-
1575815755
return ret;
1575915756
}
1576015757

@@ -15766,7 +15763,7 @@ static wc_test_ret_t aesccm_128_test(void)
1576615763
{
1576715764
wc_test_ret_t ret;
1576815765
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
15769-
Aes *enc;
15766+
Aes *enc = NULL;
1577015767
#else
1577115768
Aes enc[1];
1577215769
#endif
@@ -15867,7 +15864,7 @@ static wc_test_ret_t aesccm_128_test(void)
1586715864
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1586815865
enc = wc_AesNew(HEAP_HINT, devId);
1586915866
if (enc == NULL)
15870-
return WC_TEST_RET_ENC_ERRNO;
15867+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
1587115868
#else
1587215869
XMEMSET(enc, 0, sizeof(Aes));
1587315870
ret = wc_AesInit(enc, HEAP_HINT, devId);
@@ -15911,15 +15908,22 @@ static wc_test_ret_t aesccm_128_test(void)
1591115908
ERROR_OUT(WC_TEST_RET_ENC_NC, out);
1591215909
#endif
1591315910

15914-
XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
1591515911
XMEMSET(t2, 0, sizeof(t2));
1591615912
XMEMSET(c2, 0, sizeof(c2));
1591715913
XMEMSET(p2, 0, sizeof(p2));
1591815914
XMEMSET(iv2, 0, sizeof(iv2));
1591915915

15916+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
15917+
wc_AesFree(enc);
15918+
enc = wc_AesNew(HEAP_HINT, devId);
15919+
if (enc == NULL)
15920+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), out);
15921+
#else
15922+
XMEMSET(enc, 0, sizeof(Aes));
1592015923
ret = wc_AesInit(enc, HEAP_HINT, devId);
1592115924
if (ret != 0)
1592215925
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
15926+
#endif
1592315927

1592415928
#ifndef HAVE_SELFTEST
1592515929
/* selftest build does not have wc_AesCcmSetNonce() or

0 commit comments

Comments
 (0)