Skip to content

Commit ac74882

Browse files
chrfrankegithub-cygwin
authored andcommitted
wcstombs: also call __WCTOMB on terminating NUL if output buffer is NULL
A __WCTOMB call on the terminating NUL may emit more than a NUL byte. This is the case if the string ends with a lone UTF-16 high surrogate. Fixes: 2a3a02a ("Add SUSV2 support for calculating size if output buffer is NULL") Signed-off-by: Christian Franke <[email protected]> (cherry picked from commit fa272e05bbd049bb39420c6c8997700397283100)
1 parent 61cc419 commit ac74882

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

newlib/libc/stdlib/wcstombs_r.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ _wcstombs_r (struct _reent *r,
1717
if (s == NULL)
1818
{
1919
size_t num_bytes = 0;
20-
while (*pwcs != 0)
20+
do
2121
{
22-
bytes = __WCTOMB (r, buff, *pwcs++, state);
22+
bytes = __WCTOMB (r, buff, *pwcs, state);
2323
if (bytes == -1)
2424
return -1;
2525
num_bytes += bytes;
2626
}
27-
return num_bytes;
27+
while (*pwcs++ != 0x00);
28+
return num_bytes - 1;
2829
}
2930
else
3031
{

0 commit comments

Comments
 (0)