From 23febbd3c5d6b4e367faee4f39b8e8c3b95ab588 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 5 May 2025 09:27:29 -0400 Subject: align mbsnrtowcs behavior on partial character with new requirements POSIX 2024 added a requirement that mbsnrtowcs, like mbrtowc, consume any final partial character and store it in the mbstate_t object before returning. this was previously unspecified but documented as a potential future change. an internal mbstate_t object is added for the case where the argument is a null pointer. previously this was not needed since no operations could modify the internal object and not processing it at all gave the same behavior "as if" there were an internal object. --- src/multibyte/mbsnrtowcs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/multibyte/mbsnrtowcs.c b/src/multibyte/mbsnrtowcs.c index 931192e2..47cbdc00 100644 --- a/src/multibyte/mbsnrtowcs.c +++ b/src/multibyte/mbsnrtowcs.c @@ -2,11 +2,13 @@ size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, size_t wn, mbstate_t *restrict st) { + static unsigned internal_state; size_t l, cnt=0, n2; wchar_t *ws, wbuf[256]; const char *s = *src; const char *tmp_s; + if (!st) st = (void *)&internal_state; if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf; else ws = wcs; @@ -41,8 +43,8 @@ size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, si s = 0; break; } - /* have to roll back partial character */ - *(unsigned *)st = 0; + s += n; + n -= n; break; } s += l; n -= l; -- cgit v1.2.1