summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorjvoisin2023-06-14 14:59:11 +0200
committerjvoisin2023-06-14 14:59:11 +0200
commitcb1ce9e1815a492de0f13c2b046b8472024b9f6d (patch)
tree77b3a8268151533ad4dc7acec73d25d2144cd991 /include
parent58168afc8b2328c24137820c5fbe7c9775901944 (diff)
Add tests for strncpy and handle overlapping buffers there
Diffstat (limited to 'include')
-rw-r--r--include/string.h7
1 files changed, 6 insertions, 1 deletions
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)
156__access (read_only, 2, 3) 156__access (read_only, 2, 3)
157_FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n) 157_FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
158{ 158{
159 size_t __b = __bos(__d, 0); 159 /* trap if pointers are overlapping but not if dst == src.
160 * gcc seems to like to generate code that relies on dst == src */
161 if ((__d < __s && __d + __n > __s) ||
162 (__s < __d && __s + __n > __d))
163 __builtin_trap();
160 164
165 size_t __b = __bos(__d, 0);
161 if (__n > __b) 166 if (__n > __b)
162 __builtin_trap(); 167 __builtin_trap();
163 return __orig_strncpy(__d, __s, __n); 168 return __orig_strncpy(__d, __s, __n);