diff options
| author | jvoisin | 2023-09-30 00:15:26 +0200 |
|---|---|---|
| committer | jvoisin | 2023-09-30 00:17:34 +0200 |
| commit | a2745278eb03d63f4df8da4367f27ec8cab7deb9 (patch) | |
| tree | da05948feff6d60878ac17cd2a91d315ad8067c2 | |
| parent | 4988174f7eecc6da544ebf23156ce638cecf0f3f (diff) | |
Add `strlen`
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | include/string.h | 17 | ||||
| -rw-r--r-- | tests/Makefile | 1 | ||||
| -rw-r--r-- | tests/test_strlen_static_read.c | 22 |
4 files changed, 41 insertions, 0 deletions
| @@ -97,6 +97,7 @@ At this point, the program will safely crash. | |||
| 97 | - `strcpy` | 97 | - `strcpy` |
| 98 | - `strlcat` | 98 | - `strlcat` |
| 99 | - `strlcpy` | 99 | - `strlcpy` |
| 100 | - `strlen` | ||
| 100 | - `strncat` | 101 | - `strncat` |
| 101 | - `strncpy` | 102 | - `strncpy` |
| 102 | - `strrchr` | 103 | - `strrchr` |
diff --git a/include/string.h b/include/string.h index e681181..ab7ac43 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -35,6 +35,7 @@ extern "C" { | |||
| 35 | #undef memset | 35 | #undef memset |
| 36 | #undef strcat | 36 | #undef strcat |
| 37 | #undef strcpy | 37 | #undef strcpy |
| 38 | #undef strlen | ||
| 38 | #undef strncat | 39 | #undef strncat |
| 39 | #undef strncpy | 40 | #undef strncpy |
| 40 | 41 | ||
| @@ -237,6 +238,22 @@ _FORTIFY_FN(strcpy) char *strcpy(char * _FORTIFY_POS0 __d, const char *__s) | |||
| 237 | #endif | 238 | #endif |
| 238 | } | 239 | } |
| 239 | 240 | ||
| 241 | __access (read_only, 1) | ||
| 242 | #if __has_builtin(__builtin_strlen) | ||
| 243 | __diagnose_as_builtin(__builtin_strlen, 1) | ||
| 244 | #endif | ||
| 245 | _FORTIFY_FN(strlen) size_t strlen(const char * _FORTIFY_POS0 __s) | ||
| 246 | { | ||
| 247 | #if __has_builtin(__builtin___strlen_chk) && USE_NATIVE_CHK | ||
| 248 | return __builtin___strlen_chk(__s, __bos(__s, 0)); | ||
| 249 | #else | ||
| 250 | size_t ret = __orig_strlen(__s); | ||
| 251 | if (ret > __bos(__s, 0) - 1) | ||
| 252 | __builtin_trap(); | ||
| 253 | return ret; | ||
| 254 | #endif | ||
| 255 | } | ||
| 256 | |||
| 240 | __access (read_write, 1) | 257 | __access (read_write, 1) |
| 241 | __access (read_only, 2, 3) | 258 | __access (read_only, 2, 3) |
| 242 | #if __has_builtin(__builtin_strncat) | 259 | #if __has_builtin(__builtin_strncat) |
diff --git a/tests/Makefile b/tests/Makefile index 81593cf..c862c52 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -87,6 +87,7 @@ RUNTIME_TARGETS= \ | |||
| 87 | test_stpncpy_overwrite_over \ | 87 | test_stpncpy_overwrite_over \ |
| 88 | test_stpncpy_overwrite_under \ | 88 | test_stpncpy_overwrite_under \ |
| 89 | test_stpncpy_static_write \ | 89 | test_stpncpy_static_write \ |
| 90 | test_strlen_static_read \ | ||
| 90 | test_strcat_static_write \ | 91 | test_strcat_static_write \ |
| 91 | test_strchr_dynamic_read \ | 92 | test_strchr_dynamic_read \ |
| 92 | test_strchr_static_read \ | 93 | test_strchr_static_read \ |
diff --git a/tests/test_strlen_static_read.c b/tests/test_strlen_static_read.c new file mode 100644 index 0000000..b1f928d --- /dev/null +++ b/tests/test_strlen_static_read.c | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #define _BSD_SOURCE | ||
| 3 | |||
| 4 | #include "common.h" | ||
| 5 | |||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | int main(int argc, char** argv) { | ||
| 9 | char* canary1 = "ABCDEFGHIJKLMNOPQ"; | ||
| 10 | char buf[] = {'a', 'b', 'c', 'd', '\0'}; | ||
| 11 | char* canary2 = "ABCDEF"; | ||
| 12 | strlen(buf); | ||
| 13 | puts(buf); | ||
| 14 | buf[4] = 'e'; | ||
| 15 | |||
| 16 | CHK_FAIL_START | ||
| 17 | strlen(buf); | ||
| 18 | CHK_FAIL_END | ||
| 19 | |||
| 20 | puts(argv[1]); | ||
| 21 | return ret; | ||
| 22 | } | ||
