diff options
| author | jvoisin | 2023-06-22 18:14:24 +0200 |
|---|---|---|
| committer | jvoisin | 2023-06-22 18:14:24 +0200 |
| commit | b714dbd77f79262b00298fcdb3f69bf518dcd242 (patch) | |
| tree | 143567b7ca04389f6a5b80a24afa0139e32be5b5 | |
| parent | 532f4bfd0ba906e5a1410b9d2a46cf8a4992f062 (diff) | |
Add a test for strncat
| -rw-r--r-- | include/string.h | 5 | ||||
| -rw-r--r-- | tests/Makefile | 1 | ||||
| -rw-r--r-- | tests/test_strncat_static_write.c | 16 |
3 files changed, 19 insertions, 3 deletions
diff --git a/include/string.h b/include/string.h index 9bf17a0..8737f97 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -151,13 +151,12 @@ __access (read_only, 2, 3) | |||
| 151 | _FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n) | 151 | _FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n) |
| 152 | { | 152 | { |
| 153 | size_t __b = __bos(__d, 0); | 153 | size_t __b = __bos(__d, 0); |
| 154 | size_t __sl, __dl; | ||
| 155 | 154 | ||
| 156 | if (__n > __b) { | 155 | if (__n > __b) { |
| 157 | __sl = strlen(__s); | 156 | size_t __sl = strlen(__s); |
| 158 | __dl = strlen(__d); | ||
| 159 | if (__sl > __n) | 157 | if (__sl > __n) |
| 160 | __sl = __n; | 158 | __sl = __n; |
| 159 | size_t __dl = strlen(__d); | ||
| 161 | if (__sl + __dl + 1 > __b) | 160 | if (__sl + __dl + 1 > __b) |
| 162 | __builtin_trap(); | 161 | __builtin_trap(); |
| 163 | } | 162 | } |
diff --git a/tests/Makefile b/tests/Makefile index 11718cc..dcfe22e 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -19,6 +19,7 @@ TARGETS=test_memcpy_static_write \ | |||
| 19 | test_strcpy_overwrite_over \ | 19 | test_strcpy_overwrite_over \ |
| 20 | test_strcpy_overwrite_under \ | 20 | test_strcpy_overwrite_under \ |
| 21 | test_strcpy_static_write \ | 21 | test_strcpy_static_write \ |
| 22 | test_strncat_static_write \ | ||
| 22 | test_strncpy_overwrite_over \ | 23 | test_strncpy_overwrite_over \ |
| 23 | test_strncpy_overwrite_under \ | 24 | test_strncpy_overwrite_under \ |
| 24 | test_strncpy_static_write \ | 25 | test_strncpy_static_write \ |
diff --git a/tests/test_strncat_static_write.c b/tests/test_strncat_static_write.c new file mode 100644 index 0000000..9332adc --- /dev/null +++ b/tests/test_strncat_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 | strncat(buffer, "1234567", 5); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | strncat(buffer, "1234567890", 10); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
