From: shridhar kalavagunta Date: Tue, 30 Apr 2024 01:59:57 +0000 (-0500) Subject: Fix mem leak in threadpool_test.c X-Git-Tag: openssl-3.4.0-alpha1~538 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4dbd4925dfc61d93df678df607504f62b0ac3dcc;p=thirdparty%2Fopenssl.git Fix mem leak in threadpool_test.c Fixes #24104 Added a goto label for cleanup. Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/24412) --- diff --git a/test/threadpool_test.c b/test/threadpool_test.c index 90ddaa9ce7c..070c7cbbfb4 100644 --- a/test/threadpool_test.c +++ b/test/threadpool_test.c @@ -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