Browse Source

Fix for use of unititlized `PKCS7.isDynamic` case in unit test. Added return code checks for `wc_PKCS7_Init`.

David Garske 6 years ago
parent
commit
fb3d3dce0e
2 changed files with 11 additions and 3 deletions
  1. 1 0
      tests/api.c
  2. 10 3
      wolfcrypt/src/pkcs7.c

+ 1 - 0
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);

+ 10 - 3
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) {