STORE: fix possible memory leak
authorRichard Levitte <levitte@openssl.org>
Tue, 4 Jul 2017 15:18:31 +0000 (17:18 +0200)
committerRichard Levitte <levitte@openssl.org>
Tue, 4 Jul 2017 16:00:09 +0000 (18:00 +0200)
If scheme is NULL, the allocated res is leaked

Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3841)

crypto/store/store_register.c

index 7af1925..b366b19 100644 (file)
@@ -30,12 +30,7 @@ DEFINE_RUN_ONCE_STATIC(do_registry_init)
 
 OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme)
 {
-    OSSL_STORE_LOADER *res = OPENSSL_zalloc(sizeof(*res));
-
-    if (res == NULL) {
-        OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW, ERR_R_MALLOC_FAILURE);
-        return NULL;
-    }
+    OSSL_STORE_LOADER *res = NULL;
 
     /*
      * We usually don't check NULL arguments.  For loaders, though, the
@@ -49,6 +44,11 @@ OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme)
         return NULL;
     }
 
+    if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL) {
+        OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
+
     res->engine = e;
     res->scheme = scheme;
     return res;