summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorjvoisin2023-09-11 20:23:12 +0200
committerjvoisin2023-09-11 20:23:12 +0200
commitb2c20e6c16278fe556ce4c4ff7d3b146b3a8f009 (patch)
treeb2d0596b1cfec9344cff603f0ae6b79a8f12d97e /include
parentb9bd9400d2a6a4484ef92d708f363b9e8d0e3cb7 (diff)
Factorize overlap checks into a macro
Diffstat (limited to 'include')
-rw-r--r--include/fortify-headers.h9
-rw-r--r--include/string.h25
2 files changed, 14 insertions, 20 deletions
diff --git a/include/fortify-headers.h b/include/fortify-headers.h
index 5be4271..3eaed7c 100644
--- a/include/fortify-headers.h
+++ b/include/fortify-headers.h
@@ -123,6 +123,15 @@
123 123
124#endif /* __has_attribute */ 124#endif /* __has_attribute */
125 125
126//TODO(jvoisin) Add a check for overflows
127/* check if pointers are overlapping but not if dst == src,
128 * since gcc seems to like to generate code that relies on dst == src */
129#define __fh_overlap(a, b, l) \
130 ( \
131 ((a) < (b) && (b) < (a) + (size_t)(l)) \
132 || ((b) < (a) && (a) < (b) + (size_t)(l)) \
133 )
134
126/* 135/*
127 * We're not making use of C23's <stdckdint.h> since: 136 * We're not making use of C23's <stdckdint.h> since:
128 * - there is no elegant way to make it ignore the results. 137 * - there is no elegant way to make it ignore the results.
diff --git a/include/string.h b/include/string.h
index 342bde0..0a9cd07 100644
--- a/include/string.h
+++ b/include/string.h
@@ -49,10 +49,7 @@ _FORTIFY_FN(memcpy) void *memcpy(void * _FORTIFY_POS0 __od,
49 char *__d = (char *)__od; 49 char *__d = (char *)__od;
50 const char *__s = (const char *)__os; 50 const char *__s = (const char *)__os;
51 51
52 /* trap if pointers are overlapping but not if dst == src. 52 if __fh_overlap(__d, __s, __n)
53 * gcc seems to like to generate code that relies on dst == src */
54 if ((__d < __s && __d + __n > __s) ||
55 (__s < __d && __s + __n > __d))
56 __builtin_trap(); 53 __builtin_trap();
57 if (__n > __bd || __n > __bs) 54 if (__n > __bd || __n > __bs)
58 __builtin_trap(); 55 __builtin_trap();
@@ -131,10 +128,7 @@ _FORTIFY_FN(stpcpy) char *stpcpy(char * _FORTIFY_POS0 __d, const char *__s)
131{ 128{
132 size_t __n = strlen(__s) + 1; 129 size_t __n = strlen(__s) + 1;
133 130
134 /* trap if pointers are overlapping but not if dst == src. 131 if (__fh_overlap(__d, __s, __n))
135 * gcc seems to like to generate code that relies on dst == src */
136 if ((__d < __s && __d + __n > __s) ||
137 (__s < __d && __s + __n > __d))
138 __builtin_trap(); 132 __builtin_trap();
139 133
140 size_t __b = __bos(__d, 0); 134 size_t __b = __bos(__d, 0);
@@ -150,10 +144,7 @@ __diagnose_as_builtin(__builtin_stpncpy, 1, 2, 3)
150_FORTIFY_FN(stpncpy) char *stpncpy(char * _FORTIFY_POS0 __d, const char *__s, 144_FORTIFY_FN(stpncpy) char *stpncpy(char * _FORTIFY_POS0 __d, const char *__s,
151 size_t __n) 145 size_t __n)
152{ 146{
153 /* trap if pointers are overlapping but not if dst == src. 147 if (__fh_overlap(__d, __s, __n))
154 * gcc seems to like to generate code that relies on dst == src */
155 if ((__d < __s && __d + __n > __s) ||
156 (__s < __d && __s + __n > __d))
157 __builtin_trap(); 148 __builtin_trap();
158 149
159 size_t __b = __bos(__d, 0); 150 size_t __b = __bos(__d, 0);
@@ -182,10 +173,7 @@ _FORTIFY_FN(strcpy) char *strcpy(char * _FORTIFY_POS0 __d, const char *__s)
182{ 173{
183 size_t __n = strlen(__s) + 1; 174 size_t __n = strlen(__s) + 1;
184 175
185 /* trap if pointers are overlapping but not if dst == src. 176 if (__fh_overlap(__d, __s, __n))
186 * gcc seems to like to generate code that relies on dst == src */
187 if ((__d < __s && __d + __n > __s) ||
188 (__s < __d && __s + __n > __d))
189 __builtin_trap(); 177 __builtin_trap();
190 178
191 size_t __b = __bos(__d, 0); 179 size_t __b = __bos(__d, 0);
@@ -219,10 +207,7 @@ __diagnose_as_builtin(__builtin_strncpy, 1, 2, 3)
219_FORTIFY_FN(strncpy) char *strncpy(char * _FORTIFY_POS0 __d, 207_FORTIFY_FN(strncpy) char *strncpy(char * _FORTIFY_POS0 __d,
220 const char *__s, size_t __n) 208 const char *__s, size_t __n)
221{ 209{
222 /* trap if pointers are overlapping but not if dst == src. 210 if (__fh_overlap(__d, __s, __n))
223 * gcc seems to like to generate code that relies on dst == src */
224 if ((__d < __s && __d + __n > __s) ||
225 (__s < __d && __s + __n > __d))
226 __builtin_trap(); 211 __builtin_trap();
227 212
228 size_t __b = __bos(__d, 0); 213 size_t __b = __bos(__d, 0);