diff options
| author | jvoisin | 2023-06-14 14:59:11 +0200 |
|---|---|---|
| committer | jvoisin | 2023-06-14 14:59:11 +0200 |
| commit | cb1ce9e1815a492de0f13c2b046b8472024b9f6d (patch) | |
| tree | 77b3a8268151533ad4dc7acec73d25d2144cd991 /tests | |
| parent | 58168afc8b2328c24137820c5fbe7c9775901944 (diff) | |
Add tests for strncpy and handle overlapping buffers there
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Makefile | 33 | ||||
| -rw-r--r-- | tests/test_strncpy_overwrite_over.c | 15 | ||||
| -rw-r--r-- | tests/test_strncpy_overwrite_under.c | 15 | ||||
| -rw-r--r-- | tests/test_strncpy_static_write.c | 16 |
4 files changed, 64 insertions, 15 deletions
diff --git a/tests/Makefile b/tests/Makefile index 2c4e067..80a4626 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -2,21 +2,24 @@ CC=../x86_64-linux-musl-native/bin/gcc | |||
| 2 | GCOV=../x86_64-linux-musl-native/bin/gcov | 2 | GCOV=../x86_64-linux-musl-native/bin/gcov |
| 3 | CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 | 3 | CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 |
| 4 | 4 | ||
| 5 | TARGETS=test_memcpy_static_write \ | 5 | TARGETS=test_memcpy_static_write \ |
| 6 | test_memcpy_dynamic_write \ | 6 | test_memcpy_dynamic_write \ |
| 7 | test_memcpy_static_read \ | 7 | test_memcpy_static_read \ |
| 8 | test_memcpy_dynamic_read \ | 8 | test_memcpy_dynamic_read \ |
| 9 | test_memmove_static_write \ | 9 | test_memmove_static_write \ |
| 10 | test_memmove_dynamic_write \ | 10 | test_memmove_dynamic_write \ |
| 11 | test_memmove_static_read \ | 11 | test_memmove_static_read \ |
| 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_strcpy_static_write \ |
| 16 | test_strcat_static_write \ | 16 | test_strcat_static_write \ |
| 17 | test_strcpy_overwrite_over \ | 17 | test_strcpy_overwrite_over \ |
| 18 | test_strcpy_overwrite_under\ | 18 | test_strcpy_overwrite_under \ |
| 19 | test_getcwd \ | 19 | test_strncpy_static_write \ |
| 20 | test_strncpy_overwrite_over \ | ||
| 21 | test_strncpy_overwrite_under \ | ||
| 22 | test_getcwd \ | ||
| 20 | 23 | ||
| 21 | .SILENT: | 24 | .SILENT: |
| 22 | 25 | ||
diff --git a/tests/test_strncpy_overwrite_over.c b/tests/test_strncpy_overwrite_over.c new file mode 100644 index 0000000..d584bcc --- /dev/null +++ b/tests/test_strncpy_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 | strncpy(buffer+1, buffer, 5); | ||
| 11 | CHK_FAIL_END | ||
| 12 | |||
| 13 | puts(buffer); | ||
| 14 | return ret; | ||
| 15 | } | ||
diff --git a/tests/test_strncpy_overwrite_under.c b/tests/test_strncpy_overwrite_under.c new file mode 100644 index 0000000..f554b28 --- /dev/null +++ b/tests/test_strncpy_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 | strncpy(buffer-1, buffer, 5); | ||
| 11 | CHK_FAIL_END | ||
| 12 | |||
| 13 | puts(buffer); | ||
| 14 | return ret; | ||
| 15 | } | ||
diff --git a/tests/test_strncpy_static_write.c b/tests/test_strncpy_static_write.c new file mode 100644 index 0000000..e619288 --- /dev/null +++ b/tests/test_strncpy_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 | strncpy(buffer, "1234567", 5); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | strncpy(buffer, "1234567890", 10); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
