Skip to content

Commit 23febbd

Browse files
committed
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.
1 parent 6915b34 commit 23febbd

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/multibyte/mbsnrtowcs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, size_t wn, mbstate_t *restrict st)
44
{
5+
static unsigned internal_state;
56
size_t l, cnt=0, n2;
67
wchar_t *ws, wbuf[256];
78
const char *s = *src;
89
const char *tmp_s;
910

11+
if (!st) st = (void *)&internal_state;
1012
if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf;
1113
else ws = wcs;
1214

@@ -41,8 +43,8 @@ size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, si
4143
s = 0;
4244
break;
4345
}
44-
/* have to roll back partial character */
45-
*(unsigned *)st = 0;
46+
s += n;
47+
n -= n;
4648
break;
4749
}
4850
s += l; n -= l;

0 commit comments

Comments
 (0)