This is an automated email from the ASF dual-hosted git repository. jerpelea pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push: new fdc0b608b5 libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard fdc0b608b5 is described below commit fdc0b608b5893a207ea37a276e0adade6ded3b62 Author: Tiago Medicci Serrano <tiago.medi...@espressif.com> AuthorDate: Wed Feb 5 14:12:28 2025 -0300 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. --- libs/libc/wchar/lib_wcsrtombs.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libs/libc/wchar/lib_wcsrtombs.c b/libs/libc/wchar/lib_wcsrtombs.c index 70e6768a09..28c51ad6dc 100644 --- a/libs/libc/wchar/lib_wcsrtombs.c +++ b/libs/libc/wchar/lib_wcsrtombs.c @@ -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; }