diff options
Diffstat (limited to 'include/stdlib.h')
| -rw-r--r-- | include/stdlib.h | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/include/stdlib.h b/include/stdlib.h index 4a5cfde..6081170 100644 --- a/include/stdlib.h +++ b/include/stdlib.h | |||
| @@ -24,19 +24,41 @@ __extension__ | |||
| 24 | 24 | ||
| 25 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 | 25 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 |
| 26 | 26 | ||
| 27 | #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | ||
| 28 | #if !defined(__cplusplus) && !defined(__clang__) | ||
| 29 | __extension__ | ||
| 30 | #endif | ||
| 31 | #include_next <limits.h> | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #include "fortify-headers.h" | 27 | #include "fortify-headers.h" |
| 35 | 28 | ||
| 36 | #ifdef __cplusplus | 29 | #ifdef __cplusplus |
| 37 | extern "C" { | 30 | extern "C" { |
| 38 | #endif | 31 | #endif |
| 39 | 32 | ||
| 33 | #undef mbstowcs | ||
| 34 | #if __has_builtin(__builtin_mbstowcs) | ||
| 35 | __diagnose_as_builtin(__builtin_mbstowcs, 1, 2, 3) | ||
| 36 | #endif | ||
| 37 | _FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t * _FORTIFY_POS0 __ws, | ||
| 38 | const char *__s, size_t __wn) | ||
| 39 | { | ||
| 40 | __fh_size_t __b = __bos(__ws, 0); | ||
| 41 | |||
| 42 | if (__ws && __wn > __b / sizeof(wchar_t)) | ||
| 43 | __builtin_trap(); | ||
| 44 | return __orig_mbstowcs(__ws, __s, __wn); | ||
| 45 | } | ||
| 46 | |||
| 47 | #undef wcstombs | ||
| 48 | __access(write_only, 1, 3) | ||
| 49 | #if __has_builtin(__builtin_wcstombs) | ||
| 50 | __diagnose_as_builtin(__builtin_wcstombs, 1, 2, 3) | ||
| 51 | #endif | ||
| 52 | _FORTIFY_FN(wcstombs) size_t wcstombs(char * _FORTIFY_POS0 __s, | ||
| 53 | const wchar_t *__ws, size_t __n) | ||
| 54 | { | ||
| 55 | __fh_size_t __b = __bos(__s, 0); | ||
| 56 | |||
| 57 | if (__s && __n > __b) | ||
| 58 | __builtin_trap(); | ||
| 59 | return __orig_wcstombs(__s, __ws, __n); | ||
| 60 | } | ||
| 61 | |||
| 40 | #undef qsort | 62 | #undef qsort |
| 41 | #if __has_builtin(__builtin_qsort) | 63 | #if __has_builtin(__builtin_qsort) |
| 42 | __diagnose_as_builtin(__builtin_qsort, 1, 2, 3, 4) | 64 | __diagnose_as_builtin(__builtin_qsort, 1, 2, 3, 4) |
| @@ -45,7 +67,7 @@ __access(read_write, 1) | |||
| 45 | _FORTIFY_FN(qsort) void qsort(void * _FORTIFY_POS0 base, size_t nmemb, size_t size, | 67 | _FORTIFY_FN(qsort) void qsort(void * _FORTIFY_POS0 base, size_t nmemb, size_t size, |
| 46 | int (*compar)(const void *, const void *)) | 68 | int (*compar)(const void *, const void *)) |
| 47 | { | 69 | { |
| 48 | size_t __b = __bos(base, 0); | 70 | __fh_size_t __b = __bos(base, 0); |
| 49 | 71 | ||
| 50 | if (__bmo(nmemb, size)) | 72 | if (__bmo(nmemb, size)) |
| 51 | __builtin_trap(); | 73 | __builtin_trap(); |
| @@ -113,12 +135,10 @@ __diagnose_as_builtin(__builtin_realpath, 1, 2) | |||
| 113 | #endif | 135 | #endif |
| 114 | _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) | 136 | _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) |
| 115 | { | 137 | { |
| 116 | #ifndef PATH_MAX | 138 | // PATH_MAX is defined as 4096 |
| 117 | #error PATH_MAX unset. A fortified realpath will not work. | 139 | if (__r && 4096 > __bos(__r, 2)) { |
| 118 | #else | 140 | char __buf[4096], *__ret; |
| 119 | if (__r && PATH_MAX > __bos(__r, 2)) { | 141 | __fh_size_t __l; |
| 120 | char __buf[PATH_MAX], *__ret; | ||
| 121 | size_t __l; | ||
| 122 | 142 | ||
| 123 | __ret = __orig_realpath(__p, __buf); | 143 | __ret = __orig_realpath(__p, __buf); |
| 124 | if (!__ret) | 144 | if (!__ret) |
| @@ -130,7 +150,6 @@ _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) | |||
| 130 | return __r; | 150 | return __r; |
| 131 | } | 151 | } |
| 132 | return __orig_realpath(__p, __r); | 152 | return __orig_realpath(__p, __r); |
| 133 | #endif | ||
| 134 | } | 153 | } |
| 135 | #endif | 154 | #endif |
| 136 | 155 | ||
