From cb1ce9e1815a492de0f13c2b046b8472024b9f6d Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 14 Jun 2023 14:59:11 +0200 Subject: Add tests for strncpy and handle overlapping buffers there --- include/string.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/string.h') diff --git a/include/string.h b/include/string.h index 9b6e601..f416a51 100644 --- a/include/string.h +++ b/include/string.h @@ -156,8 +156,13 @@ __access (write_only, 1) __access (read_only, 2, 3) _FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n) { - size_t __b = __bos(__d, 0); + /* trap if pointers are overlapping but not if dst == src. + * gcc seems to like to generate code that relies on dst == src */ + if ((__d < __s && __d + __n > __s) || + (__s < __d && __s + __n > __d)) + __builtin_trap(); + size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); return __orig_strncpy(__d, __s, __n); -- cgit v1.3