[uclibc-ng-devel,30/32] mbtowc: Fix non compliant behavior for end of string

Message ID 20180704160941.3261-1-christophe.lyon@st.com
State New
Headers show
Series
  • Untitled series #53742
Related show

Commit Message

Christophe Lyon July 4, 2018, 4:09 p.m.
Match glibc behavior.

	* libc/stdlib/stdlib.c (mbtowc): Fix end of string behavior.

Signed-off-by: Mickaël Guêné <mickael.guene@st.com>
Signed-off-by: Christophe Lyon <christophe.lyon@st.com>

Patch

diff --git a/libc/stdlib/stdlib.c b/libc/stdlib/stdlib.c
index 075e6e5..f593663 100644
--- a/libc/stdlib/stdlib.c
+++ b/libc/stdlib/stdlib.c
@@ -895,9 +895,13 @@  int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
 		return is_stateful(ENCODING);
 	}
 
-	if (*s == '\0')
+	if (*s == '\0') {
 		/* According to the ISO C 89 standard this is the expected behaviour.  */
+		/* Standard not very clear here, so do like glibc.  */
+        	if (pwc != NULL)
+	        	*pwc = L'\0';
 		return 0;
+    }
 
 	if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
 		/* TODO: Should we set an error state? */