summaryrefslogtreecommitdiff
path: root/tests/test_wcsncat_safe.c
diff options
context:
space:
mode:
authorjvoisin2026-05-01 00:36:32 +0200
committerjvoisin2026-05-01 00:44:53 +0200
commitddd22b2f533db9c0da0bb262fbafa51f67c8587e (patch)
treed319dab03de20929f95ccf7f9bec8c428ab6a66b /tests/test_wcsncat_safe.c
parentd6105aba5fd791e8d3f069e771517cdb947b5604 (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_safe.c')
-rw-r--r--tests/test_wcsncat_safe.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/test_wcsncat_safe.c b/tests/test_wcsncat_safe.c
new file mode 100644
index 0000000..00fc8e6
--- /dev/null
+++ b/tests/test_wcsncat_safe.c
@@ -0,0 +1,34 @@
1#include "common.h"
2
3#include <wchar.h>
4
5int main(int argc, char** argv) {
6 wchar_t buffer[8] = {0};
7
8 /* Safe: empty buffer, append 7 wide chars → exactly fills buffer */
9 wcsncat(buffer, L"ABCDEFG", 7);
10 printf("%ls\n", buffer);
11
12 /* Safe: reset, append with n larger than source.
13 * src is L"AB" (len 2), n=100 → only 2 wchars copied */
14 buffer[0] = L'\0';
15 wcsncat(buffer, L"AB", 100);
16 printf("%ls\n", buffer);
17
18 /* Safe: partially filled, append fits exactly.
19 * buffer = L"ABCD" (4 wchars), append L"EFG" n=3 → 4+3+1 = 8 = exact fit */
20 buffer[0] = L'\0';
21 wcsncat(buffer, L"ABCD", 4);
22 wcsncat(buffer, L"EFG", 3);
23 printf("%ls\n", buffer);
24
25 /* Safe: n limits copy to fit.
26 * buffer = L"ABCDE" (5 wchars), src long, n=2 → 5+2+1 = 8 = exact fit */
27 buffer[0] = L'\0';
28 wcsncat(buffer, L"ABCDE", 5);
29 wcsncat(buffer, L"FGHIJKLM", 2);
30 printf("%ls\n", buffer);
31
32 /* All safe operations passed without trapping */
33 return 0;
34}