Commit 9a6af0f614 for openssl.org

commit 9a6af0f6142f6a3ac656622a45e9e3df65fc2042
Author: Eugene Syromiatnikov <esyr@openssl.org>
Date:   Tue May 12 15:07:17 2026 +0200

    test/mem_alloc_test.c: fix my_malloc/my_realloc behaviour on size == 0

    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)

diff --git a/test/mem_alloc_test.c b/test/mem_alloc_test.c
index 600d4fc4d0..c6ae139ae1 100644
--- a/test/mem_alloc_test.c
+++ b/test/mem_alloc_test.c
@@ -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