> New module 'mbsrtowcs'. There is a portability problem, due to incomplete specification of the function in ISO C 99. This patch documents it.
2008-12-21 Bruno Haible <br...@clisp.org> Work around a portability problem. * tests/test-mbsrtowcs.c (main): Use a temporary conversion state. * doc/posix-functions/mbsrtowcs.texi: Document the portability problem. --- doc/posix-functions/mbsrtowcs.texi.orig 2008-12-21 12:02:22.000000000 +0100 +++ doc/posix-functions/mbsrtowcs.texi 2008-12-21 11:50:37.000000000 +0100 @@ -18,4 +18,11 @@ @item On Windows platforms, @code{wchar_t} is a 16-bit type and therefore cannot accommodate all Unicode characters. +...@item +The specification is not clear about whether this function should update the +conversion state when the first argument (the destination pointer) is NULL. +The glibc implementation does not update the state in this case; the MacOS X +and FreeBSD implementations do. +For portability, when passing a NULL destination argument, it is best to pass +a pointer to a temporary copy of the conversion state. @end itemize --- tests/test-mbsrtowcs.c.orig 2008-12-21 12:02:22.000000000 +0100 +++ tests/test-mbsrtowcs.c 2008-12-21 11:53:07.000000000 +0100 @@ -88,6 +88,7 @@ #define BUFSIZE 10 wchar_t buf[BUFSIZE]; const char *src; + mbstate_t temp_state; { size_t i; @@ -118,7 +119,8 @@ input[1] = '\0'; src = input + 2; - ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 1, &state); + temp_state = state; + ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 1, &temp_state); ASSERT (ret == 3); ASSERT (src == input + 2); ASSERT (mbsinit (&state)); @@ -162,7 +164,8 @@ input[1] = '\0'; src = input + 2; - ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &state); + temp_state = state; + ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &temp_state); ASSERT (ret == 4); ASSERT (src == input + 2); ASSERT (!mbsinit (&state)); @@ -215,7 +218,8 @@ input[3] = '\0'; src = input + 4; - ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &state); + temp_state = state; + ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &temp_state); ASSERT (ret == 3); ASSERT (src == input + 4); ASSERT (!mbsinit (&state)); @@ -259,7 +263,8 @@ input[1] = '\0'; src = input + 2; - ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &state); + temp_state = state; + ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &temp_state); ASSERT (ret == 4); ASSERT (src == input + 2); ASSERT (!mbsinit (&state));