diff --git a/tests/api.c b/tests/api.c index 736313308..3d27f0b9d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -14278,6 +14278,7 @@ static void test_wc_PKCS7_InitWithCert (void) #endif printf(testingFmt, "wc_PKCS7_InitWithCert()"); /* If initialization is not successful, it's free'd in init func. */ + pkcs7.isDynamic = 0; AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (byte*)cert, (word32)certSz), 0); wc_PKCS7_Free(&pkcs7); diff --git a/wolfcrypt/src/pkcs7.c b/wolfcrypt/src/pkcs7.c index 76e398b43..89fc9992d 100644 --- a/wolfcrypt/src/pkcs7.c +++ b/wolfcrypt/src/pkcs7.c @@ -233,8 +233,13 @@ PKCS7* wc_PKCS7_New(void* heap, int devId) PKCS7* pkcs7 = (PKCS7*)XMALLOC(sizeof(PKCS7), heap, DYNAMIC_TYPE_PKCS7); if (pkcs7) { XMEMSET(pkcs7, 0, sizeof(PKCS7)); - wc_PKCS7_Init(pkcs7, heap, devId); - pkcs7->isDynamic = 1; + if (wc_PKCS7_Init(pkcs7, heap, devId) == 0) { + pkcs7->isDynamic = 1; + } + else { + XFREE(pkcs7, heap, DYNAMIC_TYPE_PKCS7); + pkcs7 = NULL; + } } return pkcs7; } @@ -284,7 +289,9 @@ int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz) heap = pkcs7->heap; devId = pkcs7->devId; isDynamic = pkcs7->isDynamic; - wc_PKCS7_Init(pkcs7, heap, devId); + ret = wc_PKCS7_Init(pkcs7, heap, devId); + if (ret != 0) + return ret; pkcs7->isDynamic = isDynamic; if (cert != NULL && certSz > 0) {