From fb7afcfee97f474382fbd47f212448f47e7f9d35 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Sat, 5 Jul 2025 12:31:26 +0200 Subject: [PATCH] Add strndup ENOMEM test --- test/val/lib_common_strndup.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/val/lib_common_strndup.c b/test/val/lib_common_strndup.c index 5b3859b37..4172af23a 100644 --- a/test/val/lib_common_strndup.c +++ b/test/val/lib_common_strndup.c @@ -1,4 +1,5 @@ #include +#include #include "unittest.h" #define SHORT_STR "abcdefghijklmnopqrstuvwxyz" @@ -56,5 +57,17 @@ TEST free(dst); } + src = malloc(LONG_STR_LEN+1); + ASSERT_IsTrue(src != NULL, "Could not allocate source string"); + memset(src, 'a', LONG_STR_LEN); + src[LONG_STR_LEN] = '\0'; + + dst = strndup(src, 30); + ASSERT_IsTrue(dst != NULL, "strndup returned NULL"); + free(dst); + + dst = strndup(src, LONG_STR_LEN); + ASSERT_IsTrue(errno == ENOMEM, "error is not ENOMEM"); + ASSERT_IsTrue(dst == NULL, "strndup did not return NULL"); } ENDTEST