diff options
| author | jvoisin | 2023-06-22 17:58:58 +0200 |
|---|---|---|
| committer | jvoisin | 2023-06-22 17:58:58 +0200 |
| commit | af7480d0190cb5dcf279a7ddfab320ff084a3471 (patch) | |
| tree | 3ba3190adb08bd5d3a4671358c5ae4c3c13f3ab2 | |
| parent | cb1ce9e1815a492de0f13c2b046b8472024b9f6d (diff) | |
Add tests for stpcpy
| -rw-r--r-- | include/string.h | 13 | ||||
| -rw-r--r-- | tests/Makefile | 7 | ||||
| -rw-r--r-- | tests/test_stpcpy_overwrite_over.c | 15 | ||||
| -rw-r--r-- | tests/test_stpcpy_overwrite_under.c | 15 | ||||
| -rw-r--r-- | tests/test_stpcpy_static_write.c | 16 |
5 files changed, 61 insertions, 5 deletions
diff --git a/include/string.h b/include/string.h index f416a51..f738901 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -85,9 +85,16 @@ __access(write_only, 1) | |||
| 85 | __access(read_only, 2) | 85 | __access(read_only, 2) |
| 86 | _FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s) | 86 | _FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s) |
| 87 | { | 87 | { |
| 88 | size_t __b = __bos(__d, 0); | 88 | size_t __n = strlen(__s) + 1; |
| 89 | 89 | ||
| 90 | if (strlen(__s) + 1 > __b) | 90 | /* trap if pointers are overlapping but not if dst == src. |
| 91 | * gcc seems to like to generate code that relies on dst == src */ | ||
| 92 | if ((__d < __s && __d + __n > __s) || | ||
| 93 | (__s < __d && __s + __n > __d)) | ||
| 94 | __builtin_trap(); | ||
| 95 | |||
| 96 | size_t __b = __bos(__d, 0); | ||
| 97 | if (__n > __b) | ||
| 91 | __builtin_trap(); | 98 | __builtin_trap(); |
| 92 | return __orig_stpcpy(__d, __s); | 99 | return __orig_stpcpy(__d, __s); |
| 93 | } | 100 | } |
| @@ -129,7 +136,7 @@ _FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s) | |||
| 129 | __builtin_trap(); | 136 | __builtin_trap(); |
| 130 | 137 | ||
| 131 | size_t __b = __bos(__d, 0); | 138 | size_t __b = __bos(__d, 0); |
| 132 | if (strlen(__s) + 1 > __b) | 139 | if (__n > __b) |
| 133 | __builtin_trap(); | 140 | __builtin_trap(); |
| 134 | return __orig_strcpy(__d, __s); | 141 | return __orig_strcpy(__d, __s); |
| 135 | } | 142 | } |
diff --git a/tests/Makefile b/tests/Makefile index 80a4626..6722add 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -12,13 +12,16 @@ TARGETS=test_memcpy_static_write \ | |||
| 12 | test_memmove_dynamic_read \ | 12 | test_memmove_dynamic_read \ |
| 13 | test_memset_static_write \ | 13 | test_memset_static_write \ |
| 14 | test_memset_dynamic_write \ | 14 | test_memset_dynamic_write \ |
| 15 | test_strcpy_static_write \ | 15 | test_stpcpy_overwrite_over \ |
| 16 | test_stpcpy_overwrite_under \ | ||
| 17 | test_stpcpy_static_write \ | ||
| 16 | test_strcat_static_write \ | 18 | test_strcat_static_write \ |
| 17 | test_strcpy_overwrite_over \ | 19 | test_strcpy_overwrite_over \ |
| 18 | test_strcpy_overwrite_under \ | 20 | test_strcpy_overwrite_under \ |
| 19 | test_strncpy_static_write \ | 21 | test_strcpy_static_write \ |
| 20 | test_strncpy_overwrite_over \ | 22 | test_strncpy_overwrite_over \ |
| 21 | test_strncpy_overwrite_under \ | 23 | test_strncpy_overwrite_under \ |
| 24 | test_strncpy_static_write \ | ||
| 22 | test_getcwd \ | 25 | test_getcwd \ |
| 23 | 26 | ||
| 24 | .SILENT: | 27 | .SILENT: |
diff --git a/tests/test_stpcpy_overwrite_over.c b/tests/test_stpcpy_overwrite_over.c new file mode 100644 index 0000000..b7cbedc --- /dev/null +++ b/tests/test_stpcpy_overwrite_over.c | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[9] = {'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', '\0'}; | ||
| 7 | puts(buffer); | ||
| 8 | |||
| 9 | CHK_FAIL_START | ||
| 10 | stpcpy(buffer+1, buffer); | ||
| 11 | CHK_FAIL_END | ||
| 12 | |||
| 13 | puts(buffer); | ||
| 14 | return ret; | ||
| 15 | } | ||
diff --git a/tests/test_stpcpy_overwrite_under.c b/tests/test_stpcpy_overwrite_under.c new file mode 100644 index 0000000..2759e5d --- /dev/null +++ b/tests/test_stpcpy_overwrite_under.c | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[9] = {'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', '\0'}; | ||
| 7 | puts(buffer); | ||
| 8 | |||
| 9 | CHK_FAIL_START | ||
| 10 | stpcpy(buffer-1, buffer); | ||
| 11 | CHK_FAIL_END | ||
| 12 | |||
| 13 | puts(buffer); | ||
| 14 | return ret; | ||
| 15 | } | ||
diff --git a/tests/test_stpcpy_static_write.c b/tests/test_stpcpy_static_write.c new file mode 100644 index 0000000..4aa4aee --- /dev/null +++ b/tests/test_stpcpy_static_write.c | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[8] = {0}; | ||
| 7 | strcpy(buffer, "1234567"); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | stpcpy(buffer, "1234567890"); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
