]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Fix mem leak in threadpool_test.c
authorshridhar kalavagunta <coolshrid@hotmail.com>
Tue, 30 Apr 2024 01:59:57 +0000 (20:59 -0500)
committerTomas Mraz <tomas@openssl.org>
Mon, 20 May 2024 08:11:35 +0000 (10:11 +0200)
Fixes #24104

Added a goto label for cleanup.

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24412)

test/threadpool_test.c

index 90ddaa9ce7c3ff948193eb13c3c5447b7a22df3c..070c7cbbfb4f91c5fd66f7952b7d3bb68d4d501e 100644 (file)
@@ -100,6 +100,7 @@ static int test_thread_internal(void)
     uint32_t threads_supported;
     size_t i;
     void *t[3];
+    int status = 0;
     OSSL_LIB_CTX *cust_ctx = OSSL_LIB_CTX_new();
 
     threads_supported = OSSL_get_thread_support_flags();
@@ -107,65 +108,66 @@ static int test_thread_internal(void)
 
     if (threads_supported == 0) {
         if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
-            return 0;
+            goto cleanup;
         if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
-            return 0;
+            goto cleanup;
 
         if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 0))
-            return 0;
+            goto cleanup;
         if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 0))
-            return 0;
+            goto cleanup;
 
         if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
-            return 0;
+            goto cleanup;
         if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
-            return 0;
+            goto cleanup;
 
         t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
         if (!TEST_ptr_null(t[0]))
-            return 0;
+            goto cleanup;
 
-        return 1;
+        status = 1;
+        goto cleanup;
     }
 
     /* fail when not allowed to use threads */
 
     if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
-        return 0;
+        goto cleanup;
     t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
     if (!TEST_ptr_null(t[0]))
-        return 0;
+        goto cleanup;
 
     /* fail when enabled on a different context */
     if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
-        return 0;
+        goto cleanup;
     if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 1))
-        return 0;
+        goto cleanup;
     if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
-        return 0;
+        goto cleanup;
     if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 1))
-        return 0;
+        goto cleanup;
     t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
     if (!TEST_ptr_null(t[0]))
-        return 0;
+        goto cleanup;
     if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 0), 1))
-        return 0;
+        goto cleanup;
 
     /* sequential startup */
 
     if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 1))
-        return 0;
+        goto cleanup;
     if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 1))
-        return 0;
+        goto cleanup;
     if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
-        return 0;
+        goto cleanup;
 
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         local[0] = i + 1;
 
         t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
         if (!TEST_ptr(t[i]))
-            return 0;
+            goto cleanup;
 
         /*
          * pthread_join results in undefined behaviour if called on a joined
@@ -174,70 +176,72 @@ static int test_thread_internal(void)
          * if we do).
          */
         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
-            return 0;
+            goto cleanup;
         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
-            return 0;
+            goto cleanup;
 
         if (!TEST_int_eq(retval[0], i + 1) || !TEST_int_eq(local[0], i + 2))
-            return 0;
+            goto cleanup;
 
         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
-            return 0;
+            goto cleanup;
         t[i] = NULL;
 
         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 0))
-            return 0;
+            goto cleanup;
     }
 
     /* parallel startup */
 
     if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t)), 1))
-        return 0;
+        goto cleanup;
 
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         local[i] = i + 1;
         t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
         if (!TEST_ptr(t[i]))
-            return 0;
+            goto cleanup;
     }
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
-            return 0;
+            goto cleanup;
     }
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
-            return 0;
+            goto cleanup;
         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
-            return 0;
+            goto cleanup;
     }
 
     /* parallel startup, bottleneck */
 
     if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t) - 1), 1))
-        return 0;
+        goto cleanup;
 
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         local[i] = i + 1;
         t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
         if (!TEST_ptr(t[i]))
-            return 0;
+            goto cleanup;
     }
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
-            return 0;
+            goto cleanup;
     }
     for (i = 0; i < OSSL_NELEM(t); ++i) {
         if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
-            return 0;
+            goto cleanup;
         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
-            return 0;
+            goto cleanup;
     }
 
     if (!TEST_int_eq(OSSL_set_max_threads(NULL, 0), 1))
-        return 0;
+        goto cleanup;
 
+    status = 1;
+cleanup:
     OSSL_LIB_CTX_free(cust_ctx);
-    return 1;
+    return status;
 }
 # endif