fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit bcc398c14af23c2d640d6fbbc6330d64e5ffad58
parent 5960e3364f54eebfbfe509abc6c629d26171734b
Author: sin <sin@2f30.org>
Date:   Wed, 28 Jan 2015 17:12:12 +0000

Simplify checks

Do not perform checks only when `n' is constant, most of the time
it is not.

The bos == (size_t)-1 check is redundant because n > bos
with bos == -1 will always be false.

Diffstat:
Minclude/string.h | 38++++++++------------------------------
1 file changed, 8 insertions(+), 30 deletions(-)

diff --git a/include/string.h b/include/string.h @@ -10,9 +10,7 @@ void *__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return memcpy(dest, src, n); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return memcpy(dest, src, n); } @@ -22,9 +20,7 @@ void *__fortify_memmove(void *__restrict dest, const void *__restrict src, size_ { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return memmove(dest, src, n); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return memmove(dest, src, n); } @@ -34,9 +30,7 @@ void *__fortify_memset(void *dest, int c, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return memset(dest, c, n); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return memset(dest, c, n); } @@ -46,8 +40,6 @@ char *__fortify_stpcpy(char *__restrict dest, const char *__restrict src) { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return stpcpy(dest, src); if (strlen(src) + 1 > bos) __builtin_trap(); return stpcpy(dest, src); @@ -58,9 +50,7 @@ char *__fortify_stpncpy(char *__restrict dest, const char *__restrict src, size_ { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return stpcpy(dest, src); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return stpncpy(dest, src, n); } @@ -70,8 +60,6 @@ char *__fortify_strcat(char *__restrict dest, const char *__restrict src) { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return strcat(dest, src); if (strlen(src) + strlen(dest) + 1 > bos) __builtin_trap(); return strcat(dest, src); @@ -82,8 +70,6 @@ char *__fortify_strcpy(char *__restrict dest, const char *__restrict src) { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return strcpy(dest, src); if (strlen(src) + 1 > bos) __builtin_trap(); return strcpy(dest, src); @@ -95,9 +81,7 @@ char *__fortify_strncat(char *__restrict dest, const char *__restrict src, size_ size_t bos = __builtin_object_size(dest, 0); size_t slen, dlen; - if (bos == (size_t)-1) - return strncat(dest, src, n); - if (__builtin_constant_p(n) && n > bos) { + if (n > bos) { slen = strlen(src); dlen = strlen(dest); if (slen > n) @@ -113,9 +97,7 @@ char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_ { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return strncpy(dest, src, n); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return strncpy(dest, src, n); } @@ -126,9 +108,7 @@ size_t __fortify_strlcat(char *__restrict dest, const char *__restrict src, size { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return strlcat(dest, src, n); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return strlcat(dest, src, n); } @@ -138,9 +118,7 @@ size_t __fortify_strlcpy(char *__restrict dest, const char *__restrict src, size { size_t bos = __builtin_object_size(dest, 0); - if (bos == (size_t)-1) - return strlcpy(dest, src, n); - if (__builtin_constant_p(n) && n > bos) + if (n > bos) __builtin_trap(); return strlcpy(dest, src, n); }