commit 8513fddefca4c6e3982718732afeec71bad4e688
parent 80a83a56b52e833e6d3afec4d0723d7625d52cee
Author: jvoisin <julien.voisin@dustri.org>
Date: Wed, 27 Dec 2023 16:17:55 +0100
Improve __fh_overlap implementation
Diffstat:
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/include/fortify-headers.h b/include/fortify-headers.h
@@ -135,10 +135,10 @@
//TODO(jvoisin) Add a check for overflows
/* check if pointers are overlapping but not if dst == src,
* since gcc seems to like to generate code that relies on dst == src */
-#define __fh_overlap(a, b, l) \
+#define __fh_overlap(a, len_a, b, len_b) \
( \
- ((a) < (b) && (b) < (a) + (__fh_size_t)(l)) \
- || ((b) < (a) && (a) < (b) + (__fh_size_t)(l)) \
+ ((a) < (b) && (b) < (a) + (__fh_size_t)(len_a)) \
+ || ((b) < (a) && (a) < (b) + (__fh_size_t)(len_b)) \
)
/*
diff --git a/include/string.h b/include/string.h
@@ -56,7 +56,7 @@ __error_if((__fh_bos(__od, 0) < __n), "'memcpy' called with `n` bigger than the
char *__d = (char *)__od;
const char *__s = (const char *)__os;
- if __fh_overlap(__d, __s, __n)
+ if __fh_overlap(__d, __bd, __s, __n)
__builtin_trap();
if (__n > __bd || __n > __bs)
__builtin_trap();
@@ -166,11 +166,11 @@ _FORTIFY_FN(stpcpy) char *stpcpy(char * _FORTIFY_POS0 __d, const char *__s)
return __builtin___stpcpy_chk(__d, __s, __fh_bos(__d, 0));
#else
__fh_size_t __n = strlen(__s) + 1;
+ __fh_size_t __b = __fh_bos(__d, 0);
- if (__fh_overlap(__d, __s, __n))
+ if (__fh_overlap(__d, __b, __s, __n))
__builtin_trap();
- __fh_size_t __b = __fh_bos(__d, 0);
if (__n > __b)
__builtin_trap();
return __orig_stpcpy(__d, __s);
@@ -236,11 +236,11 @@ _FORTIFY_FN(strcpy) char *strcpy(char * _FORTIFY_POS0 __d, const char *__s)
return __builtin___strcpy_chk(__d, __s, __fh_bos(__d, 0));
#else
__fh_size_t __n = strlen(__s) + 1;
+ __fh_size_t __b = __fh_bos(__d, 0);
- if (__fh_overlap(__d, __s, __n))
+ if (__fh_overlap(__d, __b, __s, __n))
__builtin_trap();
- __fh_size_t __b = __fh_bos(__d, 0);
if (__n > __b)
__builtin_trap();
return __orig_strcpy(__d, __s);