diff options
| author | jvoisin | 2026-05-01 00:36:32 +0200 |
|---|---|---|
| committer | jvoisin | 2026-05-01 00:44:53 +0200 |
| commit | ddd22b2f533db9c0da0bb262fbafa51f67c8587e (patch) | |
| tree | d319dab03de20929f95ccf7f9bec8c428ab6a66b /tests/test_wcsncat_n_lt_buf.c | |
| parent | d6105aba5fd791e8d3f069e771517cdb947b5604 (diff) | |
Fix strncat/wcsncat
Previously, no checks were done when __n <= __b, but strncat _appends_ after
existing content, making this a overly broad check check. For example, with an
8-byte buffer containing "12345\0", strncat(buf, "ABCD", 4) would have the
check skipped, but the result "12345ABCD\0" is 10 bytes, resulting in an
overflow.
This commit fixes this oversight, and adds a bunch of tests.
Diffstat (limited to 'tests/test_wcsncat_n_lt_buf.c')
| -rw-r--r-- | tests/test_wcsncat_n_lt_buf.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/test_wcsncat_n_lt_buf.c b/tests/test_wcsncat_n_lt_buf.c new file mode 100644 index 0000000..99a42de --- /dev/null +++ b/tests/test_wcsncat_n_lt_buf.c | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <wchar.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | wchar_t buffer[8] = {0}; | ||
| 7 | wcsncat(buffer, L"12345", 5); | ||
| 8 | printf("%ls\n", buffer); | ||
| 9 | |||
| 10 | /* n < buffer capacity but overflow due to existing content. | ||
| 11 | * buffer has 5 wchars, src L"ABCD" (len 4), min(4,3)=3: 5+3+1 = 9 > 8. */ | ||
| 12 | CHK_FAIL_START | ||
| 13 | wcsncat(buffer, L"ABCD", 3); | ||
| 14 | CHK_FAIL_END | ||
| 15 | |||
| 16 | printf("%ls\n", buffer); | ||
| 17 | return ret; | ||
| 18 | } | ||
