]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
test/mem_alloc_test.c: fix my_malloc/my_realloc behaviour on size == 0
authorEugene Syromiatnikov <esyr@openssl.org>
Tue, 12 May 2026 13:07:17 +0000 (15:07 +0200)
committerNorbert Pocs <norbertp@openssl.org>
Mon, 18 May 2026 07:30:43 +0000 (09:30 +0200)
That puts them more in line with CRYPTO_malloc() and CRYPTO_realloc()
behaviour, whose behaviour for the requested size of 0 is well-documented
and not "implementation-defined", as POSIX allows.

Fixes: d090695101a9 "test: add a sanity test for memory allocation functions"
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
MergeDate: Mon May 18 07:30:49 2026
(Merged from https://github.com/openssl/openssl/pull/31158)

test/mem_alloc_test.c

index 600d4fc4d008cfeae545b68ec190123e0500bb43..c6ae139ae1889b2a9d4731840cc1eae816dec8dc 100644 (file)
@@ -188,7 +188,7 @@ static int secure_memory_is_secure;
 static void *my_malloc(const size_t num,
     const char *const file, const int line)
 {
-    void *const p = malloc(num);
+    void *const p = num > 0 ? malloc(num) : NULL;
 
 #if CUSTOM_FN_PRINT_CALLS
     if (file == test_fn || file == NULL
@@ -201,13 +201,21 @@ static void *my_malloc(const size_t num,
 
     return p;
 }
+
 static void *my_realloc(void *const addr, const size_t num,
     const char *const file, const int line)
 {
 #if CUSTOM_FN_PRINT_CALLS
     const uintptr_t old_addr = (uintptr_t)addr;
 #endif
-    void *const p = realloc(addr, num);
+    void *p = NULL;
+
+    if (addr == NULL && num > 0)
+        p = malloc(num);
+    else if (num == 0)
+        free(addr);
+    else
+        p = realloc(addr, num);
 
 #if CUSTOM_FN_PRINT_CALLS
     if (file == test_fn || file == NULL