fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit b714dbd77f79262b00298fcdb3f69bf518dcd242
parent 532f4bfd0ba906e5a1410b9d2a46cf8a4992f062
Author: jvoisin <julien.voisin@dustri.org>
Date:   Thu, 22 Jun 2023 18:14:24 +0200

Add a test for strncat

Diffstat:
Minclude/string.h | 5++---
Mtests/Makefile | 1+
Atests/test_strncat_static_write.c | 16++++++++++++++++
3 files changed, 19 insertions(+), 3 deletions(-)

diff --git 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 @@ -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 @@ -0,0 +1,16 @@ +#include "common.h" + +#include <string.h> + +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; +}