diff options
Diffstat (limited to 'include/string.h')
| -rw-r--r-- | include/string.h | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/include/string.h b/include/string.h new file mode 100644 index 0000000..944cf0b --- /dev/null +++ b/include/string.h | |||
| @@ -0,0 +1,238 @@ | |||
| 1 | #ifndef FORTIFY_STRING_H_ | ||
| 2 | #define FORTIFY_STRING_H_ | ||
| 3 | |||
| 4 | #include_next <string.h> | ||
| 5 | |||
| 6 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 | ||
| 7 | |||
| 8 | static inline __attribute__ ((always_inline)) | ||
| 9 | void *__memcpy_chk(void *__restrict dest, const void *__restrict src, size_t ssize, | ||
| 10 | size_t dsize) | ||
| 11 | { | ||
| 12 | if (ssize > dsize) | ||
| 13 | __builtin_trap(); | ||
| 14 | return memcpy(dest, src, ssize); | ||
| 15 | } | ||
| 16 | |||
| 17 | static inline __attribute__ ((always_inline)) | ||
| 18 | void *__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) | ||
| 19 | { | ||
| 20 | size_t bos = __builtin_object_size(dest, 0); | ||
| 21 | |||
| 22 | if (bos == (size_t)-1) | ||
| 23 | return memcpy(dest, src, n); | ||
| 24 | if (__builtin_constant_p(n)) | ||
| 25 | if (n <= bos) | ||
| 26 | return memcpy(dest, src, n); | ||
| 27 | return __memcpy_chk(dest, src, n, bos); | ||
| 28 | } | ||
| 29 | |||
| 30 | #undef memcpy | ||
| 31 | #define memcpy(dest, src, n) __fortify_memcpy(dest, src, n) | ||
| 32 | |||
| 33 | static inline __attribute__ ((always_inline)) | ||
| 34 | void *__memmove_chk(void *__restrict dest, const void *__restrict src, size_t ssize, | ||
| 35 | size_t dsize) | ||
| 36 | { | ||
| 37 | if (ssize > dsize) | ||
| 38 | __builtin_trap(); | ||
| 39 | return memmove(dest, src, ssize); | ||
| 40 | } | ||
| 41 | |||
| 42 | static inline __attribute__ ((always_inline)) | ||
| 43 | void *__fortify_memmove(void *__restrict dest, const void *__restrict src, size_t n) | ||
| 44 | { | ||
| 45 | size_t bos = __builtin_object_size(dest, 0); | ||
| 46 | |||
| 47 | if (bos == (size_t)-1) | ||
| 48 | return memmove(dest, src, n); | ||
| 49 | if (__builtin_constant_p(n)) | ||
| 50 | if (n <= bos) | ||
| 51 | return memmove(dest, src, n); | ||
| 52 | return __memmove_chk(dest, src, n, bos); | ||
| 53 | } | ||
| 54 | |||
| 55 | #undef memmove | ||
| 56 | #define memmove(dest, src, n) __fortify_memmove(dest, src, n) | ||
| 57 | |||
| 58 | static inline __attribute__ ((always_inline)) | ||
| 59 | void *__memset_chk(void *dest, int c, size_t n, size_t dsize) | ||
| 60 | { | ||
| 61 | if (n > dsize) | ||
| 62 | __builtin_trap(); | ||
| 63 | return memset(dest, c, n); | ||
| 64 | } | ||
| 65 | |||
| 66 | static inline __attribute__ ((always_inline)) | ||
| 67 | void *__fortify_memset(void *dest, int c, size_t n) | ||
| 68 | { | ||
| 69 | size_t bos = __builtin_object_size(dest, 0); | ||
| 70 | |||
| 71 | if (bos == (size_t)-1) | ||
| 72 | return memset(dest, c, n); | ||
| 73 | if (__builtin_constant_p(n)) | ||
| 74 | if (n <= bos) | ||
| 75 | return memset(dest, c, n); | ||
| 76 | return __memset_chk(dest, c, n, bos); | ||
| 77 | } | ||
| 78 | |||
| 79 | #undef memset | ||
| 80 | #define memset(dest, src, n) __fortify_memset(dest, src, n) | ||
| 81 | |||
| 82 | static inline __attribute__ ((always_inline)) | ||
| 83 | char *__strcat_chk(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 84 | { | ||
| 85 | size_t slen = strlen(src); | ||
| 86 | size_t dlen = strlen(dest); | ||
| 87 | |||
| 88 | if (slen + dlen + 1 > n) | ||
| 89 | __builtin_trap(); | ||
| 90 | return strcat(dest, src); | ||
| 91 | } | ||
| 92 | |||
| 93 | static inline __attribute__ ((always_inline)) | ||
| 94 | char *__fortify_strcat(char *__restrict dest, const char *__restrict src) | ||
| 95 | { | ||
| 96 | size_t bos = __builtin_object_size(dest, 0); | ||
| 97 | |||
| 98 | if (bos == (size_t)-1) | ||
| 99 | return strcat(dest, src); | ||
| 100 | return __strcat_chk(dest, src, bos); | ||
| 101 | } | ||
| 102 | |||
| 103 | #undef strcat | ||
| 104 | #define strcat(dest, src) __fortify_strcat(dest, src) | ||
| 105 | |||
| 106 | static inline __attribute__ ((always_inline)) | ||
| 107 | char *__strcpy_chk(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 108 | { | ||
| 109 | size_t slen = strlen(src); | ||
| 110 | |||
| 111 | if (slen + 1 > n) | ||
| 112 | __builtin_trap(); | ||
| 113 | return strcpy(dest, src); | ||
| 114 | } | ||
| 115 | |||
| 116 | static inline __attribute__ ((always_inline)) | ||
| 117 | char *__fortify_strcpy(char *__restrict dest, const char *__restrict src) | ||
| 118 | { | ||
| 119 | size_t bos = __builtin_object_size(dest, 0); | ||
| 120 | |||
| 121 | if (bos == (size_t)-1) | ||
| 122 | return strcpy(dest, src); | ||
| 123 | return __strcpy_chk(dest, src, bos); | ||
| 124 | } | ||
| 125 | |||
| 126 | #undef strcpy | ||
| 127 | #define strcpy(dest, src) __fortify_strcpy(dest, src) | ||
| 128 | |||
| 129 | static inline __attribute__ ((always_inline)) | ||
| 130 | char *__strncat_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 131 | size_t dsize) | ||
| 132 | { | ||
| 133 | size_t slen = strlen(src); | ||
| 134 | size_t dlen = strlen(dest); | ||
| 135 | |||
| 136 | if (slen > n) slen = n; | ||
| 137 | if (slen + dlen + 1 > dsize) | ||
| 138 | __builtin_trap(); | ||
| 139 | return strncat(dest, src, n); | ||
| 140 | } | ||
| 141 | |||
| 142 | static inline __attribute__ ((always_inline)) | ||
| 143 | char *__fortify_strncat(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 144 | { | ||
| 145 | size_t bos = __builtin_object_size(dest, 0); | ||
| 146 | |||
| 147 | if (bos == (size_t)-1) | ||
| 148 | return strncat(dest, src, n); | ||
| 149 | if (__builtin_constant_p(n)) | ||
| 150 | if (n <= bos) | ||
| 151 | return strncat(dest, src, n); | ||
| 152 | return __strncat_chk(dest, src, n, bos); | ||
| 153 | } | ||
| 154 | |||
| 155 | #undef strncat | ||
| 156 | #define strncat(dest, src, n) __fortify_strcat(dest, src, n) | ||
| 157 | |||
| 158 | static inline __attribute__ ((always_inline)) | ||
| 159 | char *__strncpy_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 160 | size_t dsize) | ||
| 161 | { | ||
| 162 | if (n > dsize) | ||
| 163 | __builtin_trap(); | ||
| 164 | return strncpy(dest, src, n); | ||
| 165 | } | ||
| 166 | |||
| 167 | static inline __attribute__ ((always_inline)) | ||
| 168 | char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 169 | { | ||
| 170 | size_t bos = __builtin_object_size(dest, 0); | ||
| 171 | |||
| 172 | if (bos == (size_t)-1) | ||
| 173 | return strncpy(dest, src, n); | ||
| 174 | if (__builtin_constant_p(n)) | ||
| 175 | if (n <= bos) | ||
| 176 | return strncpy(dest, src, n); | ||
| 177 | return __strncpy_chk(dest, src, n, bos); | ||
| 178 | } | ||
| 179 | |||
| 180 | #undef strncpy | ||
| 181 | #define strncpy(dest, src, n) __fortify_strcpy(dest, src, n) | ||
| 182 | |||
| 183 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | ||
| 184 | static inline __attribute__ ((always_inline)) | ||
| 185 | size_t __strlcat_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 186 | size_t dsize) | ||
| 187 | { | ||
| 188 | if (n > dsize) | ||
| 189 | __builtin_trap(); | ||
| 190 | return strlcat(dest, src, n); | ||
| 191 | } | ||
| 192 | |||
| 193 | static inline __attribute__ ((always_inline)) | ||
| 194 | size_t __fortify_strlcat(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 195 | { | ||
| 196 | size_t bos = __builtin_object_size(dest, 0); | ||
| 197 | |||
| 198 | if (bos == (size_t)-1) | ||
| 199 | return strlcat(dest, src, n); | ||
| 200 | if (__builtin_constant_p(n)) { | ||
| 201 | if (n <= bos) | ||
| 202 | return strlcat(dest, src, n); | ||
| 203 | } | ||
| 204 | return __strlcat_chk(dest, src, n, bos); | ||
| 205 | } | ||
| 206 | |||
| 207 | #undef strlcat | ||
| 208 | #define strlcat(dest, src, n) __fortify_strlcat(dest, src, n) | ||
| 209 | |||
| 210 | static inline __attribute__ ((always_inline)) | ||
| 211 | size_t __strlcpy_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 212 | size_t dsize) | ||
| 213 | { | ||
| 214 | if (n > dsize) | ||
| 215 | __builtin_trap(); | ||
| 216 | return strlcpy(dest, src, n); | ||
| 217 | } | ||
| 218 | |||
| 219 | static inline __attribute__ ((always_inline)) | ||
| 220 | size_t __fortify_strlcpy(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 221 | { | ||
| 222 | size_t bos = __builtin_object_size(dest, 0); | ||
| 223 | |||
| 224 | if (bos == (size_t)-1) | ||
| 225 | return strlcpy(dest, src, n); | ||
| 226 | if (__builtin_constant_p(n)) { | ||
| 227 | if (n <= bos) | ||
| 228 | return strlcpy(dest, src, n); | ||
| 229 | } | ||
| 230 | return __strlcpy_chk(dest, src, n, bos); | ||
| 231 | } | ||
| 232 | |||
| 233 | #undef strlcpy | ||
| 234 | #define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) | ||
| 235 | #endif | ||
| 236 | #endif | ||
| 237 | |||
| 238 | #endif | ||
