summaryrefslogtreecommitdiff
path: root/tests/test_wcsnrtombs_static.c
diff options
context:
space:
mode:
authorjvoisin2026-04-30 17:42:29 +0200
committerjvoisin2026-04-30 17:42:29 +0200
commitf9239e2c0f0be9856322727887a45333683940a6 (patch)
tree714b611965666c4072fef6218e7a794dff1884cb /tests/test_wcsnrtombs_static.c
parent6040b4a27409968c764353a98c45d972cfd89a8a (diff)
Fix a bug in wcsnrtombs
__d is a char * destination buffer, so __b is already the byte capacity. Dividing by sizeof(wchar_t) makes no sense here, it was likely copy-pasted from mbsnrtowcs (where the destination is wchar_t *). The first branch also fails to limit __n (the byte write cap) to __b, so overflows are possible when a wide character produces multi-byte output. The second branch (else) correctly limits __n to __b. This commit replaces the broken two-branch logic with the simple correct pattern matching wcsrtombs, and adds two tests two prove that nothing broke.
Diffstat (limited to 'tests/test_wcsnrtombs_static.c')
-rw-r--r--tests/test_wcsnrtombs_static.c26
1 files changed, 26 insertions, 0 deletions
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
6int 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}