diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Makefile | 2 | ||||
| -rw-r--r-- | tests/test_wcsnrtombs_dynamic.c | 28 | ||||
| -rw-r--r-- | tests/test_wcsnrtombs_static.c | 26 |
3 files changed, 56 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile index 71fb930..adea381 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -94,6 +94,8 @@ RUNTIME_TARGETS= \ | |||
| 94 | test_vsnprintf_static \ | 94 | test_vsnprintf_static \ |
| 95 | test_vsprintf \ | 95 | test_vsprintf \ |
| 96 | test_wcrtomb \ | 96 | test_wcrtomb \ |
| 97 | test_wcsnrtombs_dynamic \ | ||
| 98 | test_wcsnrtombs_static \ | ||
| 97 | test_wcscat_static_write \ | 99 | test_wcscat_static_write \ |
| 98 | test_wcscpy_static_write \ | 100 | test_wcscpy_static_write \ |
| 99 | test_wcsncat_static_write \ | 101 | test_wcsncat_static_write \ |
diff --git a/tests/test_wcsnrtombs_dynamic.c b/tests/test_wcsnrtombs_dynamic.c new file mode 100644 index 0000000..808c9c8 --- /dev/null +++ b/tests/test_wcsnrtombs_dynamic.c | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <wchar.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | int main(int argc, char** argv) { | ||
| 7 | char buffer[8] = {0}; | ||
| 8 | const wchar_t src[] = L"ABCD"; | ||
| 9 | const wchar_t *srcp = src; | ||
| 10 | mbstate_t st; | ||
| 11 | memset(&st, 0, sizeof(st)); | ||
| 12 | |||
| 13 | /* Safe: convert up to 4 wide chars, write at most 4 bytes */ | ||
| 14 | srcp = src; | ||
| 15 | wcsnrtombs(buffer, &srcp, 4, 4, &st); | ||
| 16 | |||
| 17 | /* Unsafe: ask to write argc (10) bytes into 8-byte buffer. | ||
| 18 | * Before the fix, the first branch incorrectly divided the byte-sized | ||
| 19 | * buffer capacity by sizeof(wchar_t), making the check too permissive. */ | ||
| 20 | CHK_FAIL_START | ||
| 21 | srcp = src; | ||
| 22 | memset(&st, 0, sizeof(st)); | ||
| 23 | wcsnrtombs(buffer, &srcp, 4, argc, &st); | ||
| 24 | CHK_FAIL_END | ||
| 25 | |||
| 26 | puts(buffer); | ||
| 27 | return ret; | ||
| 28 | } | ||
diff --git a/tests/test_wcsnrtombs_static.c b/tests/test_wcsnrtombs_static.c new file mode 100644 index 0000000..7f2883f --- /dev/null +++ b/tests/test_wcsnrtombs_static.c | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <wchar.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | int main(int argc, char** argv) { | ||
| 7 | char buffer[4] = {0}; | ||
| 8 | const wchar_t src[] = L"ABCDEFGHIJ"; | ||
| 9 | const wchar_t *srcp = src; | ||
| 10 | mbstate_t st; | ||
| 11 | memset(&st, 0, sizeof(st)); | ||
| 12 | |||
| 13 | /* Safe: convert up to 2 wide chars, write at most 2 bytes */ | ||
| 14 | srcp = src; | ||
| 15 | wcsnrtombs(buffer, &srcp, 2, 2, &st); | ||
| 16 | |||
| 17 | /* Unsafe: ask to write 16 bytes into 4-byte buffer */ | ||
| 18 | CHK_FAIL_START | ||
| 19 | srcp = src; | ||
| 20 | memset(&st, 0, sizeof(st)); | ||
| 21 | wcsnrtombs(buffer, &srcp, 10, 16, &st); | ||
| 22 | CHK_FAIL_END | ||
| 23 | |||
| 24 | puts(buffer); | ||
| 25 | return ret; | ||
| 26 | } | ||
