From 986006e5674af3aa2c88fe04db9b376b6d3338e8 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 19 Sep 2023 18:30:25 +0100 Subject: [PATCH] Make TEST_CALLOC_NONNULL more robust Signed-off-by: Dave Rodgman --- tests/include/test/macros.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/include/test/macros.h b/tests/include/test/macros.h index c107ccfca7..3bfbe3333d 100644 --- a/tests/include/test/macros.h +++ b/tests/include/test/macros.h @@ -160,12 +160,18 @@ * \param item_count Number of elements to allocate. * This expression may be evaluated multiple times. * + * Note: if passing size 0, mbedtls_calloc may return NULL. In this case, + * we reattempt to allocate with the smallest possible buffer to assure a + * non-NULL pointer. */ #define TEST_CALLOC_NONNULL(pointer, item_count) \ do { \ TEST_ASSERT((pointer) == NULL); \ (pointer) = mbedtls_calloc(sizeof(*(pointer)), \ (item_count)); \ + if (((pointer) == NULL) && ((item_count) == 0)) { \ + (pointer) = mbedtls_calloc(1, 1); \ + } \ TEST_ASSERT((pointer) != NULL); \ } while (0)