fortify-headers

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

commit 57a3e9c1c7507b5ce56122429cc3649009a4ecc7
parent 8342bc50a16a2158fef729db6cbfef874734e4dc
Author: jvoisin <julien.voisin@dustri.org>
Date:   Fri, 15 Dec 2023 16:08:12 +0100

Remove superfluous includes

This should fix #32

Diffstat:
Minclude/fortify-headers.h | 8++++++--
Minclude/poll.h | 4++--
Minclude/stdio.h | 24++++++++++++------------
Minclude/stdlib.h | 49++++++++++++++++++++++++++++++++++---------------
Minclude/string.h | 46+++++++++++++++++++++++-----------------------
Minclude/strings.h | 6+++---
Minclude/sys/select.h | 6+++---
Minclude/sys/socket.h | 8++++----
Minclude/unistd.h | 28++++++++++++++--------------
Minclude/wchar.h | 106++++++++++++++++++++++++++-----------------------------------------------------
Mtests/test_mbstowcs_dynamic.c | 2+-
Mtests/test_mbstowcs_static.c | 2+-
12 files changed, 137 insertions(+), 152 deletions(-)

diff --git a/include/fortify-headers.h b/include/fortify-headers.h @@ -123,13 +123,17 @@ #endif /* __has_attribute */ +// Since we're not allow to include anything. +// https://awesomekling.github.io/How-SerenityOS-declares-ssize_t/ +#define __fh_size_t __typeof__(sizeof(char)) + //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) \ ( \ - ((a) < (b) && (b) < (a) + (size_t)(l)) \ - || ((b) < (a) && (a) < (b) + (size_t)(l)) \ + ((a) < (b) && (b) < (a) + (__fh_size_t)(l)) \ + || ((b) < (a) && (a) < (b) + (__fh_size_t)(l)) \ ) /* diff --git a/include/poll.h b/include/poll.h @@ -36,7 +36,7 @@ __diagnose_as_builtin(__builtin_poll, 1, 2, 3) #endif _FORTIFY_FN(poll) int poll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, int __s) { - size_t __b = __bos(__f, 0); + __fh_size_t __b = __bos(__f, 0); if (__n > __b / sizeof(struct pollfd)) __builtin_trap(); @@ -51,7 +51,7 @@ __diagnose_as_builtin(__builtin_ppoll, 1, 2, 3, 4) _FORTIFY_FN(ppoll) int ppoll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, const struct timespec *__s, const sigset_t *__m) { - size_t __b = __bos(__f, 0); + __fh_size_t __b = __bos(__f, 0); if (__n > __b / sizeof(struct pollfd)) __builtin_trap(); diff --git a/include/stdio.h b/include/stdio.h @@ -54,9 +54,9 @@ __diagnose_as_builtin(__builtin_fgets, 1, 2, 3) #endif _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); - if ((size_t)__n > __b) + if ((__fh_size_t)__n > __b) __builtin_trap(); return __orig_fgets(__s, __n, __f); } @@ -88,7 +88,7 @@ __diagnose_as_builtin(__builtin_fread, 1, 2, 3, 4) _FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, size_t __m, FILE *__f) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__bmo(__n, __m)) __builtin_trap(); @@ -104,7 +104,7 @@ __diagnose_as_builtin(__builtin_fwrite, 1, 2, 3, 4) _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n, size_t __m, FILE *__f) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__bmo(__n, __m)) __builtin_trap(); @@ -148,7 +148,7 @@ _FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n, #if __has_builtin(__builtin___vsnprintf_chk) && USE_NATIVE_CHK return __builtin___vsnprintf_chk(__s, __n, _FORTIFY_SOURCE, __bos(__s, 0), __f, __v); #else - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -168,12 +168,12 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, #if __has_builtin(__builtin___vsprintf_chk) && USE_NATIVE_CHK return __builtin___vsprintf_chk(__s, _FORTIFY_SOURCE, __bos(__s, 0), __f, __v); #else - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); int __r; - if (__b != (size_t)-1) { + if (__b != (__fh_size_t)-1) { __r = __orig_vsnprintf(__s, __b, __f, __v); - if (__r != -1 && (size_t)__r >= __b) + if (__r != -1 && (__fh_size_t)__r >= __b) __builtin_trap(); } else { __r = __orig_vsprintf(__s, __f, __v); @@ -250,7 +250,7 @@ _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, #if __has_builtin(__builtin___snprintf_chk) && USE_NATIVE_CHK return __builtin___snprintf_chk(__s, __n, _FORTIFY_SOURCE, __bos(__s, 0), __f, __builtin_va_arg_pack()); #else - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -266,12 +266,12 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) #if __has_builtin(__builtin___sprintf_chk) && USE_NATIVE_CHK return __builtin___sprintf_chk(__s, _FORTIFY_SOURCE, __bos(__s, 0), __f, __builtin_va_arg_pack()); #else - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); int __r; - if (__b != (size_t)-1) { + if (__b != (__fh_size_t)-1) { __r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack()); - if (__r != -1 && (size_t)__r >= __b) + if (__r != -1 && (__fh_size_t)__r >= __b) __builtin_trap(); } else { __r = __orig_sprintf(__s, __f, __builtin_va_arg_pack()); diff --git a/include/stdlib.h b/include/stdlib.h @@ -24,19 +24,41 @@ __extension__ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#if !defined(__cplusplus) && !defined(__clang__) -__extension__ -#endif -#include_next <limits.h> -#endif - #include "fortify-headers.h" #ifdef __cplusplus extern "C" { #endif +#undef mbstowcs +#if __has_builtin(__builtin_mbstowcs) +__diagnose_as_builtin(__builtin_mbstowcs, 1, 2, 3) +#endif +_FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t * _FORTIFY_POS0 __ws, + const char *__s, size_t __wn) +{ + __fh_size_t __b = __bos(__ws, 0); + + if (__ws && __wn > __b / sizeof(wchar_t)) + __builtin_trap(); + return __orig_mbstowcs(__ws, __s, __wn); +} + +#undef wcstombs +__access(write_only, 1, 3) +#if __has_builtin(__builtin_wcstombs) +__diagnose_as_builtin(__builtin_wcstombs, 1, 2, 3) +#endif +_FORTIFY_FN(wcstombs) size_t wcstombs(char * _FORTIFY_POS0 __s, + const wchar_t *__ws, size_t __n) +{ + __fh_size_t __b = __bos(__s, 0); + + if (__s && __n > __b) + __builtin_trap(); + return __orig_wcstombs(__s, __ws, __n); +} + #undef qsort #if __has_builtin(__builtin_qsort) __diagnose_as_builtin(__builtin_qsort, 1, 2, 3, 4) @@ -45,7 +67,7 @@ __access(read_write, 1) _FORTIFY_FN(qsort) void qsort(void * _FORTIFY_POS0 base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { - size_t __b = __bos(base, 0); + __fh_size_t __b = __bos(base, 0); if (__bmo(nmemb, size)) __builtin_trap(); @@ -113,12 +135,10 @@ __diagnose_as_builtin(__builtin_realpath, 1, 2) #endif _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) { -#ifndef PATH_MAX -#error PATH_MAX unset. A fortified realpath will not work. -#else - if (__r && PATH_MAX > __bos(__r, 2)) { - char __buf[PATH_MAX], *__ret; - size_t __l; + // PATH_MAX is defined as 4096 + if (__r && 4096 > __bos(__r, 2)) { + char __buf[4096], *__ret; + __fh_size_t __l; __ret = __orig_realpath(__p, __buf); if (!__ret) @@ -130,7 +150,6 @@ _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) return __r; } return __orig_realpath(__p, __r); -#endif } #endif diff --git a/include/string.h b/include/string.h @@ -51,8 +51,8 @@ __error_if((__bos(__od, 0) < __n), "'memcpy' called with `n` bigger than the siz #if __has_builtin(__builtin___memcpy_chk) && USE_NATIVE_CHK return __builtin___memcpy_chk(__od, __os, __n, __bos(__od, 0)); #else - size_t __bd = __bos(__od, 0); - size_t __bs = __bos(__os, 0); + __fh_size_t __bd = __bos(__od, 0); + __fh_size_t __bs = __bos(__os, 0); char *__d = (char *)__od; const char *__s = (const char *)__os; @@ -75,8 +75,8 @@ _FORTIFY_FN(memmove) void *memmove(void * _FORTIFY_POS0 __d, #if __has_builtin(__builtin___memmove_chk) && USE_NATIVE_CHK return __builtin___memmove_chk(__d, __s, __n, __bos(__d, 0)); #else - size_t __bd = __bos(__d, 0); - size_t __bs = __bos(__s, 0); + __fh_size_t __bd = __bos(__d, 0); + __fh_size_t __bs = __bos(__s, 0); if (__n > __bd || __n > __bs) __builtin_trap(); @@ -94,7 +94,7 @@ __warning_if(__c != 0 && __n == 0, "'memset' will set `0` bytes; did you invert #if __has_builtin(__builtin___memset_chk) && USE_NATIVE_CHK return __builtin___memset_chk(__d, __c, __n, __bos(__d, 0)); #else - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); @@ -111,7 +111,7 @@ _FORTIFY_FN(memchr) void *memchr(const void * _FORTIFY_POS0 __d, int __c, size_t #if __has_builtin(__builtin___memchr_chk) && USE_NATIVE_CHK return __builtin___memchr_chk(__d, __c, __n, __bos(__d, 0)); #else - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); @@ -125,7 +125,7 @@ _FORTIFY_FN(strchr) char *strchr(const char * _FORTIFY_POS0 __s, int __c) #if __has_builtin(__builtin___strchr_chk) && USE_NATIVE_CHK return __builtin___strchr_chk(__s, __c, __bos(__s, 0)); #else - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); char* __r = __builtin_strchr(__s, __c); if (__r - __s > __b) @@ -140,7 +140,7 @@ _FORTIFY_FN(strrchr) char *strrchr(const char * _FORTIFY_POS0 __s, int __c) #if __has_builtin(__builtin___strrchr_chk) && USE_NATIVE_CHK return __builtin___strrchr_chk(__s, __c, __bos(__s, 0)); #else - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); char* __r = __builtin_strrchr(__s, __c); if (__r - __s > __b) @@ -163,12 +163,12 @@ _FORTIFY_FN(stpcpy) char *stpcpy(char * _FORTIFY_POS0 __d, const char *__s) #if __has_builtin(__builtin___stpcpy_chk) && USE_NATIVE_CHK return __builtin___stpcpy_chk(__d, __s, __bos(__d, 0)); #else - size_t __n = strlen(__s) + 1; + __fh_size_t __n = strlen(__s) + 1; if (__fh_overlap(__d, __s, __n)) __builtin_trap(); - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); return __orig_stpcpy(__d, __s); @@ -190,7 +190,7 @@ _FORTIFY_FN(stpncpy) char *stpncpy(char * _FORTIFY_POS0 __d, const char *__s, if (__fh_overlap(__d, __s, __n)) __builtin_trap(); - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b && strlen(__s) + 1 > __b) __builtin_trap(); return __orig_stpncpy(__d, __s, __n); @@ -208,7 +208,7 @@ _FORTIFY_FN(strcat) char *strcat(char * _FORTIFY_POS0 __d, const char *__s) #if __has_builtin(__builtin___strcat_chk) && USE_NATIVE_CHK return __builtin___strcat_chk(__d, __s, __bos(__d, 0)); #else - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (strlen(__s) + strlen(__d) + 1 > __b) __builtin_trap(); @@ -226,12 +226,12 @@ _FORTIFY_FN(strcpy) char *strcpy(char * _FORTIFY_POS0 __d, const char *__s) #if __has_builtin(__builtin___strcpy_chk) && USE_NATIVE_CHK return __builtin___strcpy_chk(__d, __s, __bos(__d, 0)); #else - size_t __n = strlen(__s) + 1; + __fh_size_t __n = strlen(__s) + 1; if (__fh_overlap(__d, __s, __n)) __builtin_trap(); - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); return __orig_strcpy(__d, __s); @@ -247,7 +247,7 @@ _FORTIFY_FN(strlen) size_t strlen(const char * _FORTIFY_POS0 __s) #if __has_builtin(__builtin___strlen_chk) && USE_NATIVE_CHK return __builtin___strlen_chk(__s, __bos(__s, 0)); #else - size_t ret = __orig_strlen(__s); + __fh_size_t ret = __orig_strlen(__s); if (ret > __bos(__s, 0) - 1) __builtin_trap(); return ret; @@ -265,11 +265,11 @@ _FORTIFY_FN(strncat) char *strncat(char * _FORTIFY_POS0 __d, const char *__s, #if __has_builtin(__builtin___strncat_chk) && USE_NATIVE_CHK return __builtin___strncat_chk(__d, __s, __n, __bos(__d, 0)); #else - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) { - size_t __sl = strnlen(__s, __n); - size_t __dl = strlen(__d); + __fh_size_t __sl = strnlen(__s, __n); + __fh_size_t __dl = strlen(__d); if (__sl + __dl + 1 > __b) __builtin_trap(); } @@ -291,7 +291,7 @@ _FORTIFY_FN(strncpy) char *strncpy(char * _FORTIFY_POS0 __d, if (__fh_overlap(__d, __s, __n)) __builtin_trap(); - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); return __orig_strncpy(__d, __s, __n); @@ -311,8 +311,8 @@ _FORTIFY_FN(mempcpy) void *mempcpy(void * _FORTIFY_POS0 __d, #if __has_builtin(__builtin___mempcpy_chk) && USE_NATIVE_CHK return __builtin___mempcpy_chk(__d, __s, __n, __bos(__d, 0)); #else - size_t __bd = __bos(__d, 0); - size_t __bs = __bos(__s, 0); + __fh_size_t __bd = __bos(__d, 0); + __fh_size_t __bs = __bos(__s, 0); if (__n > __bd || __n > __bs) __builtin_trap(); @@ -335,7 +335,7 @@ _FORTIFY_FN(strlcat) size_t strlcat(char * _FORTIFY_POS0 __d, #if __has_builtin(__builtin___strlcat_chk) && USE_NATIVE_CHK return __builtin___strlcat_chk(__d, __s, __n, __bos(__d, 0)); #else - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); @@ -354,7 +354,7 @@ _FORTIFY_FN(strlcpy) size_t strlcpy(char * _FORTIFY_POS0 __d, #if __has_builtin(__builtin___strlcpy_chk) && USE_NATIVE_CHK return __builtin___strlcpy_chk(__d, __s, __n, __bos(__d, 0)); #else - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b) __builtin_trap(); diff --git a/include/strings.h b/include/strings.h @@ -40,8 +40,8 @@ __diagnose_as_builtin(__builtin_bcopy, 1, 2, 3) _FORTIFY_FN(bcopy) void bcopy(const void * _FORTIFY_POS0 __s, void * _FORTIFY_POS0 __d, size_t __n) { - size_t __bd = __bos(__d, 0); - size_t __bs = __bos(__s, 0); + __fh_size_t __bd = __bos(__d, 0); + __fh_size_t __bs = __bos(__s, 0); if (__n > __bd || __n > __bs) __builtin_trap(); @@ -54,7 +54,7 @@ __diagnose_as_builtin(__builtin_bzero, 1, 2) #endif _FORTIFY_FN(bzero) void bzero(void * _FORTIFY_POS0 __s, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); diff --git a/include/sys/select.h b/include/sys/select.h @@ -33,7 +33,7 @@ extern "C" { _STI void __fortify_FD_CLR(int __f, fd_set * _FORTIFY_POS0 __s) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__f < 0 || __f >= FD_SETSIZE || __b < sizeof(fd_set)) __builtin_trap(); @@ -42,7 +42,7 @@ _STI void __fortify_FD_CLR(int __f, fd_set * _FORTIFY_POS0 __s) _STI void __fortify_FD_SET(int __f, fd_set * _FORTIFY_POS0 __s) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__f < 0 || __f >= FD_SETSIZE || __b < sizeof(fd_set)) __builtin_trap(); @@ -51,7 +51,7 @@ _STI void __fortify_FD_SET(int __f, fd_set * _FORTIFY_POS0 __s) _STI int __fortify_FD_ISSET(int __f, fd_set * _FORTIFY_POS0 __s) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__f < 0 || __f >= FD_SETSIZE || __b < sizeof(fd_set)) __builtin_trap(); diff --git a/include/sys/socket.h b/include/sys/socket.h @@ -38,7 +38,7 @@ __access(write_only, 2, 3) _FORTIFY_FN(recv) ssize_t recv(int __f, void * _FORTIFY_POS0 __s, size_t __n, int __fl) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -50,7 +50,7 @@ _FORTIFY_FN(recvfrom) ssize_t recvfrom(int __f, void * _FORTIFY_POS0 __s, size_t __n, int __fl, struct sockaddr *__a, socklen_t *__l) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -61,7 +61,7 @@ __access(read_only, 2, 3) _FORTIFY_FN(send) ssize_t send(int __f, const void * _FORTIFY_POS0 __s, size_t __n, int __fl) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -73,7 +73,7 @@ _FORTIFY_FN(sendto) ssize_t sendto(int __f, const void * _FORTIFY_POS0 __s, size_t __n, int __fl, const struct sockaddr *__a, socklen_t __l) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); diff --git a/include/unistd.h b/include/unistd.h @@ -47,8 +47,8 @@ __diagnose_as_builtin(__builtin_confstr, 1, 2, 3) #endif _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l) { - size_t __b = __bos(__s, 0); - size_t __r = __orig_confstr(__n, __s, __b > __l ? __l : __b); + __fh_size_t __b = __bos(__s, 0); + __fh_size_t __r = __orig_confstr(__n, __s, __b > __l ? __l : __b); if (__l > __b && __r > __b) __builtin_trap(); @@ -61,7 +61,7 @@ __diagnose_as_builtin(__builtin_getcwd, 1, 2) #endif _FORTIFY_FN(getcwd) char *getcwd(char * _FORTIFY_POS0 __s, size_t __l) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__l > __b) __builtin_trap(); @@ -76,7 +76,7 @@ __diagnose_as_builtin(__builtin_getdomainname, 1, 2) #endif _FORTIFY_FN(getdomainname) int getdomainname(char * _FORTIFY_POS0 __s, size_t __l) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__l > __b) __builtin_trap(); @@ -89,7 +89,7 @@ __diagnose_as_builtin(__builtin_getgroups, 1, 2) #endif _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t * _FORTIFY_POS0 __s) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__l > 0 && (unsigned)__l > __b / sizeof(gid_t)) __builtin_trap(); @@ -102,7 +102,7 @@ __diagnose_as_builtin(__builtin_gethostname, 1, 2) #endif _FORTIFY_FN(gethostname) int gethostname(char * _FORTIFY_POS0 __s, size_t __l) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__l > __b) __builtin_trap(); @@ -115,7 +115,7 @@ __diagnose_as_builtin(__builtin_getlogin_r, 1, 2) #endif _FORTIFY_FN(getlogin_r) int getlogin_r(char * _FORTIFY_POS0 __s, size_t __l) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__l > __b) __builtin_trap(); @@ -128,7 +128,7 @@ __diagnose_as_builtin(__builtin_pread, 1, 2, 3, 4) _FORTIFY_FN(pread) ssize_t pread(int __f, void * _FORTIFY_POS0 __s, size_t __n, off_t __o) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -141,7 +141,7 @@ __diagnose_as_builtin(__builtin_read, 1, 2, 3) #endif _FORTIFY_FN(read) ssize_t read(int __f, void * _FORTIFY_POS0 __s, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -155,7 +155,7 @@ __diagnose_as_builtin(__builtin_readlink, 1, 2, 3) _FORTIFY_FN(readlink) ssize_t readlink(const char *__p, char * _FORTIFY_POS0 __s, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -169,7 +169,7 @@ __diagnose_as_builtin(__builtin_readlinkat, 1, 2, 3, 4) _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p, char * _FORTIFY_POS0 __s, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -183,7 +183,7 @@ __diagnose_as_builtin(__builtin_ttyname_r, 1, 2, 3) _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char * _FORTIFY_POS0 __s, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -197,7 +197,7 @@ __diagnose_as_builtin(__builtin_write, 1, 2, 3) _FORTIFY_FN(write) ssize_t write(int __f, const void * _FORTIFY_POS0 __s, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); @@ -210,7 +210,7 @@ __diagnose_as_builtin(__builtin_pwrite, 1, 2, 3, 4) _FORTIFY_FN(pwrite) ssize_t pwrite(int __f, const void * _FORTIFY_POS0 __s, size_t __n, off_t __o) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b) __builtin_trap(); diff --git a/include/wchar.h b/include/wchar.h @@ -20,18 +20,6 @@ #if !defined(__cplusplus) && !defined(__clang__) __extension__ #endif -#include_next <limits.h> -#if !defined(__cplusplus) && !defined(__clang__) -__extension__ -#endif -#include_next <stdlib.h> -#if !defined(__cplusplus) && !defined(__clang__) -__extension__ -#endif -#include_next <string.h> -#if !defined(__cplusplus) && !defined(__clang__) -__extension__ -#endif #include_next <wchar.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 @@ -43,15 +31,12 @@ extern "C" { #undef fgetws #undef mbsrtowcs -#undef mbstowcs #undef wcrtomb #undef wcscat #undef wcscpy #undef wcsncat #undef wcsncpy #undef wcsrtombs -#undef wcstombs -#undef wctomb #undef wmemcpy #undef wmemmove #undef wmemset @@ -62,9 +47,9 @@ __diagnose_as_builtin(__builtin_fgetws, 1, 2, 3) _FORTIFY_FN(fgetws) wchar_t *fgetws(wchar_t * _FORTIFY_POS0 __s, int __n, FILE *__f) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); - if ((size_t)__n > __b / sizeof(wchar_t)) + if ((__fh_size_t)__n > __b / sizeof(wchar_t)) __builtin_trap(); return __orig_fgetws(__s, __n, __f); } @@ -79,17 +64,17 @@ _FORTIFY_FN(mbsnrtowcs) size_t mbsnrtowcs(wchar_t * _FORTIFY_POS0 __d, const char **__s, size_t __n, size_t __wn, mbstate_t *__st) { - size_t __b = __bos(__d, 0); - size_t __r; + __fh_size_t __b = __bos(__d, 0); + __fh_size_t __r; if (__wn > __n / sizeof(wchar_t)) { __b /= sizeof(wchar_t); __r = __orig_mbsnrtowcs(__d, __s, __n, __wn > __b ? __b : __wn, __st); - if (__b < __wn && __d && *__s && __r != (size_t)-1) + if (__b < __wn && __d && *__s && __r != (__fh_size_t)-1) __builtin_trap(); } else { __r = __orig_mbsnrtowcs(__d, __s, __n > __b ? __b : __n, __wn, __st); - if (__b < __n && __d && *__s && __r != (size_t)-1) + if (__b < __n && __d && *__s && __r != (__fh_size_t)-1) __builtin_trap(); } return __r; @@ -103,29 +88,16 @@ _FORTIFY_FN(mbsrtowcs) size_t mbsrtowcs(wchar_t * _FORTIFY_POS0 __d, const char **__s, size_t __wn, mbstate_t *__st) { - size_t __b = __bos(__d, 0); - size_t __r; + __fh_size_t __b = __bos(__d, 0); + __fh_size_t __r; __b /= sizeof(wchar_t); __r = __orig_mbsrtowcs(__d, __s, __wn > __b ? __b : __wn, __st); - if (__b < __wn && __d && *__s && __r != (size_t)-1) + if (__b < __wn && __d && *__s && __r != (__fh_size_t)-1) __builtin_trap(); return __r; } -#if __has_builtin(__builtin_mbstowcs) -__diagnose_as_builtin(__builtin_mbstowcs, 1, 2, 3) -#endif -_FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t * _FORTIFY_POS0 __ws, - const char *__s, size_t __wn) -{ - size_t __b = __bos(__ws, 0); - - if (__ws && __wn > __b / sizeof(wchar_t)) - __builtin_trap(); - return __orig_mbstowcs(__ws, __s, __wn); -} - /* FIXME clang */ #ifndef __clang__ #if __has_builtin(__builtin_wcrtomb) @@ -133,12 +105,13 @@ __diagnose_as_builtin(__builtin_wcrtomb, 1, 2, 3) #endif _FORTIFY_FN(wcrtomb) size_t wcrtomb(char * __s, wchar_t __w, mbstate_t *__st) { - if (__s && MB_LEN_MAX > __bos(__s, 2)) { - char __buf[MB_LEN_MAX]; - size_t __r; + // In glibc, MB_LEN_MAX is typically 16 (6 in glibc versions earlier than 2.2) + if (__s && 16 > __bos(__s, 2)) { + char __buf[16]; + __fh_size_t __r; __r = __orig_wcrtomb(__buf, __w, __st); - if (__r == (size_t)-1) + if (__r == (__fh_size_t)-1) return __r; if (__r > __bos(__s, 0)) __builtin_trap(); @@ -155,7 +128,7 @@ __diagnose_as_builtin(__builtin_wcscat, 1, 2) _FORTIFY_FN(wcscat) wchar_t *wcscat(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (wcslen(__s) + wcslen(__d) + 1 > __b / sizeof(wchar_t)) __builtin_trap(); @@ -168,7 +141,7 @@ __diagnose_as_builtin(__builtin_wcscpy, 1, 2) _FORTIFY_FN(wcscpy) wchar_t *wcscpy(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (wcslen(__s) + 1 > __b / sizeof(wchar_t)) __builtin_trap(); @@ -181,8 +154,8 @@ __diagnose_as_builtin(__builtin_wcsncat, 1, 2, 3) _FORTIFY_FN(wcsncat) wchar_t *wcsncat(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { - size_t __b = __bos(__d, 0); - size_t __sl, __dl; + __fh_size_t __b = __bos(__d, 0); + __fh_size_t __sl, __dl; if (__n > __b / sizeof(wchar_t)) { __sl = wcslen(__s); @@ -201,7 +174,7 @@ __diagnose_as_builtin(__builtin_wcsncpy, 1, 2, 3) _FORTIFY_FN(wcsncpy) wchar_t *wcsncpy(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b / sizeof(wchar_t)) __builtin_trap(); @@ -218,17 +191,17 @@ _FORTIFY_FN(wcsnrtombs) size_t wcsnrtombs(char * _FORTIFY_POS0 __d, const wchar_t **__s, size_t __wn, size_t __n, mbstate_t *__st) { - size_t __b = __bos(__d, 0); - size_t __r; + __fh_size_t __b = __bos(__d, 0); + __fh_size_t __r; if (__wn > __n / sizeof(wchar_t)) { __b /= sizeof(wchar_t); __r = __orig_wcsnrtombs(__d, __s, __wn > __b ? __b : __wn, __n, __st); - if (__b < __wn && __d && *__s && __r != (size_t)-1) + if (__b < __wn && __d && *__s && __r != (__fh_size_t)-1) __builtin_trap(); } else { __r = __orig_wcsnrtombs(__d, __s, __wn, __n > __b ? __b : __n, __st); - if (__b < __n && __d && *__s && __r != (size_t)-1) + if (__b < __n && __d && *__s && __r != (__fh_size_t)-1) __builtin_trap(); } return __r; @@ -242,40 +215,29 @@ _FORTIFY_FN(wcsrtombs) size_t wcsrtombs(char * _FORTIFY_POS0 __d, const wchar_t **__s, size_t __n, mbstate_t *__st) { - size_t __b = __bos(__d, 0); - size_t __r; + __fh_size_t __b = __bos(__d, 0); + __fh_size_t __r; __r = __orig_wcsrtombs(__d, __s, __n > __b ? __b : __n, __st); - if (__b < __n && __d && *__s && __r != (size_t)-1) + if (__b < __n && __d && *__s && __r != (__fh_size_t)-1) __builtin_trap(); return __r; } -__access(write_only, 1, 3) -#if __has_builtin(__builtin_wcstombs) -__diagnose_as_builtin(__builtin_wcstombs, 1, 2, 3) -#endif -_FORTIFY_FN(wcstombs) size_t wcstombs(char * _FORTIFY_POS0 __s, - const wchar_t *__ws, size_t __n) -{ - size_t __b = __bos(__s, 0); - - if (__s && __n > __b) - __builtin_trap(); - return __orig_wcstombs(__s, __ws, __n); -} - +#ifdef MB_CUR_MAX +#undef wctomb #if __has_builtin(__builtin_wctomb) __diagnose_as_builtin(__builtin_wctomb, 1, 2) #endif _FORTIFY_FN(wctomb) int wctomb(char * _FORTIFY_POS0 __s, wchar_t __w) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); - if (__s && MB_LEN_MAX > __b && MB_CUR_MAX > __b) + if (__s && 16 > __b && MB_CUR_MAX > __b) __builtin_trap(); return __orig_wctomb(__s, __w); } +#endif // MB_CUR_MAX #if __has_builtin(__builtin_wmemcpy) __diagnose_as_builtin(__builtin_wmemcpy, 1, 2, 3) @@ -283,7 +245,7 @@ __diagnose_as_builtin(__builtin_wmemcpy, 1, 2, 3) _FORTIFY_FN(wmemcpy) wchar_t *wmemcpy(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b / sizeof(wchar_t)) __builtin_trap(); @@ -296,7 +258,7 @@ __diagnose_as_builtin(__builtin_wmemmove, 1, 2, 3) _FORTIFY_FN(wmemmove) wchar_t *wmemmove(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { - size_t __b = __bos(__d, 0); + __fh_size_t __b = __bos(__d, 0); if (__n > __b / sizeof(wchar_t)) __builtin_trap(); @@ -309,7 +271,7 @@ __diagnose_as_builtin(__builtin_wmemset, 1, 2, 3) _FORTIFY_FN(wmemset) wchar_t *wmemset(wchar_t * _FORTIFY_POS0 __s, wchar_t __c, size_t __n) { - size_t __b = __bos(__s, 0); + __fh_size_t __b = __bos(__s, 0); if (__n > __b / sizeof(wchar_t)) __builtin_trap(); diff --git a/tests/test_mbstowcs_dynamic.c b/tests/test_mbstowcs_dynamic.c @@ -1,6 +1,6 @@ #include "common.h" -#include <wchar.h> +#include <stdlib.h> int main(int argc, char** argv) { const char* mbstr = "z\u00df\u6c34\U0001f34c"; // or u8"zß水🍌" diff --git a/tests/test_mbstowcs_static.c b/tests/test_mbstowcs_static.c @@ -1,6 +1,6 @@ #include "common.h" -#include <wchar.h> +#include <stdlib.h> int main(int argc, char** argv) { const char* mbstr = "z\u00df\u6c34\U0001f34c"; // or u8"zß水🍌"