From 532f4bfd0ba906e5a1410b9d2a46cf8a4992f062 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 22 Jun 2023 18:07:41 +0200 Subject: Add tests for stcncpy --- 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 f738901..9bf17a0 100644 --- a/include/string.h +++ b/include/string.h @@ -104,8 +104,13 @@ __access(write_only, 1) __access(read_only, 2, 3) _FORTIFY_FN(stpncpy) char *stpncpy(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 && strlen(__s) + 1 > __b) __builtin_trap(); return __orig_stpncpy(__d, __s, __n); -- cgit v1.3