libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard

According to https://pubs.opengroup.org/onlinepubs/9799919799/, the
expected behavior for the wcstombs() function is that it should
convert the wide-character codes stoping conversion either when a
character sequence exceeds the limit of n total bytes or if a null
byte is stored. In the first case, the null-terminated value should
not be appended to the dst buffer.

Currently, no null-terminator is appended to the dst buffer, even
when it's expected to be appended, generating erroneous output.
This commit is contained in:
Tiago Medicci Serrano
2025-02-05 14:12:28 -03:00
committed by Alin Jerpelea
parent 0ae633cc08
commit fdc0b608b5
+10 -1
View File
@@ -40,5 +40,14 @@
size_t wcsrtombs(FAR char *dst, FAR const wchar_t **src,
size_t len, FAR mbstate_t *ps)
{
return wcsnrtombs(dst, src, SIZE_MAX, len, ps);
size_t ret;
ret = wcsnrtombs(dst, src, SIZE_MAX, len, ps);
if (dst != NULL && ret != (size_t)-1 && ret != len)
{
dst[ret] = '\0';
}
return ret;
}