From c19d37adf0bf156fdea7b18bf068e3aea3a1374f Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 1 Jun 2022 01:14:56 +0800 Subject: [PATCH] libc/wchar: Call mbsnrtowcs in mbrtowc to handle the partial sequence correctly Signed-off-by: Xiang Xiao --- libs/libc/wchar/lib_mbrtowc.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libs/libc/wchar/lib_mbrtowc.c b/libs/libc/wchar/lib_mbrtowc.c index 8acf296b62..25862fce05 100644 --- a/libs/libc/wchar/lib_mbrtowc.c +++ b/libs/libc/wchar/lib_mbrtowc.c @@ -57,15 +57,19 @@ size_t mbrtowc(FAR wchar_t *pwc, FAR const char *s, size_t n, FAR mbstate_t *ps) { - int retval = 0; + FAR const char *e = s; + size_t retval = 0; if (s == NULL) { - retval = mbtowc(NULL, "", 1); + s = e = ""; + n = 1; } - else + + retval = mbsnrtowcs(pwc, &e, 1, n, ps); + if (retval == 1) { - retval = mbtowc(pwc, s, n); + retval = e - s; } return retval;