From b714dbd77f79262b00298fcdb3f69bf518dcd242 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 22 Jun 2023 18:14:24 +0200 Subject: [PATCH] Add a test for strncat --- include/string.h | 5 ++--- tests/Makefile | 1 + tests/test_strncat_static_write.c | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 tests/test_strncat_static_write.c diff --git a/include/string.h b/include/string.h index 9bf17a0..8737f97 100644 --- a/include/string.h +++ b/include/string.h @@ -151,13 +151,12 @@ __access (read_only, 2, 3) _FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n) { size_t __b = __bos(__d, 0); - size_t __sl, __dl; if (__n > __b) { - __sl = strlen(__s); - __dl = strlen(__d); + size_t __sl = strlen(__s); if (__sl > __n) __sl = __n; + size_t __dl = strlen(__d); if (__sl + __dl + 1 > __b) __builtin_trap(); } diff --git a/tests/Makefile b/tests/Makefile index 11718cc..dcfe22e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -19,6 +19,7 @@ TARGETS=test_memcpy_static_write \ test_strcpy_overwrite_over \ test_strcpy_overwrite_under \ test_strcpy_static_write \ + test_strncat_static_write \ test_strncpy_overwrite_over \ test_strncpy_overwrite_under \ test_strncpy_static_write \ diff --git a/tests/test_strncat_static_write.c b/tests/test_strncat_static_write.c new file mode 100644 index 0000000..9332adc --- /dev/null +++ b/tests/test_strncat_static_write.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { + char buffer[8] = {0}; + strncat(buffer, "1234567", 5); + puts(buffer); + + CHK_FAIL_START + strncat(buffer, "1234567890", 10); + CHK_FAIL_END + + puts(buffer); + return ret; +}