]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
test: fix coverity 1414445: resource leak
authorPauli <ppzgs1@gmail.com>
Thu, 18 Mar 2021 00:39:25 +0000 (10:39 +1000)
committerPauli <ppzgs1@gmail.com>
Sat, 20 Mar 2021 00:07:59 +0000 (10:07 +1000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14596)

test/exdatatest.c

index 8dd9261d25cdc2073f4427c8493b88fa9411b9b1..dfccb6f0d1c16ca17e6d88f3d3fe307dee93e86c 100644 (file)
@@ -135,8 +135,10 @@ static MYOBJ *MYOBJ_new(void)
     static int count = 0;
     MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
 
-    obj->id = ++count;
-    obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
+    if (obj != NULL) {
+        obj->id = ++count;
+        obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
+    }
     return obj;
 }
 
@@ -199,31 +201,37 @@ static char *MYOBJ_gethello3(MYOBJ *obj)
 
 static void MYOBJ_free(MYOBJ *obj)
 {
-    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
-    OPENSSL_free(obj);
+    if (obj != NULL) {
+        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
+        OPENSSL_free(obj);
+    }
 }
 
 static MYOBJ *MYOBJ_dup(MYOBJ *in)
 {
     MYOBJ *obj = MYOBJ_new();
 
-    obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
-                                 &in->ex_data);
+    if (obj != NULL)
+        obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
+                                     &in->ex_data);
     return obj;
 }
 
 static int test_exdata(void)
 {
-    MYOBJ *t1, *t2, *t3;
-    MYOBJ_EX_DATA *ex_data;
+    MYOBJ *t1 = NULL, *t2 = NULL, *t3 = NULL;
+    MYOBJ_EX_DATA *ex_data = NULL;
     const char *cp;
     char *p;
+    int res = 0;
 
     gbl_result = 1;
 
-    p = OPENSSL_strdup("hello world");
+    if (!TEST_ptr(p = OPENSSL_strdup("hello world")))
+        return 0;
     saved_argl = 21;
-    saved_argp = OPENSSL_malloc(1);
+    if (!TEST_ptr(saved_argp = OPENSSL_malloc(1)))
+        goto err;
     saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
                                         saved_argl, saved_argp,
                                         exnew, exdup, exfree);
@@ -233,9 +241,9 @@ static int test_exdata(void)
     t1 = MYOBJ_new();
     t2 = MYOBJ_new();
     if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
-        return 0;
+        goto err;
     if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
-        return 0;
+        goto err;
 
     /*
      * saved_idx3 differs from other indexes by being created after the exdata
@@ -245,63 +253,63 @@ static int test_exdata(void)
                                          saved_argl, saved_argp,
                                          exnew2, exdup2, exfree2);
     if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
-        return 0;
+        goto err;
 
     MYOBJ_sethello(t1, p);
     cp = MYOBJ_gethello(t1);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     MYOBJ_sethello2(t1, p);
     cp = MYOBJ_gethello2(t1);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     MYOBJ_allochello3(t1, p);
     cp = MYOBJ_gethello3(t1);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello(t2);
     if (!TEST_ptr_null(cp))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello2(t2);
     if (!TEST_ptr_null(cp))
-        return 0;
+        goto err;
 
     t3 = MYOBJ_dup(t1);
     if (!TEST_int_eq(t3->st, 1))
-        return 0;
+        goto err;
 
     ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
     if (!TEST_ptr(ex_data))
-        return 0;
+        goto err;
     if (!TEST_int_eq(ex_data->dup, 1))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello(t3);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello2(t3);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello3(t3);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
+    if (gbl_result)
+        res = 1;
+ err:
     MYOBJ_free(t1);
     MYOBJ_free(t2);
     MYOBJ_free(t3);
     OPENSSL_free(saved_argp);
+    saved_argp = NULL;
     OPENSSL_free(p);
-
-    if (gbl_result)
-      return 1;
-    else
-      return 0;
+    return res;
 }
 
 int setup_tests(void)