From 836d348090eca4d96007639074f4c101388f6945 Mon Sep 17 00:00:00 2001 From: Alexey Izbyshev Date: Tue, 30 May 2023 13:52:03 +0300 Subject: [PATCH 3/3] mbsnrtowcs: don't modify conversion state if dest buf is NULL Mail-Followup-To: musl@lists.openwall.com POSIX specifies mbsnrtowcs to be an input-length-limiting equivalent of mbsrtowcs. The latter doesn't modify the passed conversion state if the destination buffer is NULL. This behavior, in particular, makes it possible to learn the required size of the destination buffer by passing NULL on the first call, allocate the buffer, and call mbsrtowcs again without the need to save/restore the conversion state manually. Since we don't care about rolling back the conversion state in this case, simply reuse the copy of the original state as a throw-away state. --- src/multibyte/mbsnrtowcs.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/multibyte/mbsnrtowcs.c b/src/multibyte/mbsnrtowcs.c index c3c1f709..bd73ff09 100644 --- a/src/multibyte/mbsnrtowcs.c +++ b/src/multibyte/mbsnrtowcs.c @@ -12,7 +12,10 @@ size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, si if (!st) st = (void *)&internal_state; st0 = *(unsigned *)st; - if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf; + if (!wcs) { + ws = wbuf, wn = sizeof wbuf / sizeof *wbuf; + st = (void *)&st0; + } else ws = wcs; /* making sure output buffer size is at most n/4 will ensure -- 2.39.2