diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs index bf345e142e..ee6aea1172 100644 --- a/src/header/wchar/mod.rs +++ b/src/header/wchar/mod.rs @@ -321,28 +321,33 @@ pub unsafe extern "C" fn wcsrtombs( } while n >= 4 { - if **ws.wrapping_sub(1) >= 0x7f { + if (**ws).wrapping_sub(1) >= 0x7f { if **ws == 0 { *s = 0; *ws = 0 as *const wchar_t; return N.wrapping_sub(n); } + l = wcrtomb(s, **ws, ptr::null_mut()); if l.wrapping_add(1) == 0 { return usize::MAX; } + s = s.offset(l as isize); n = n.wrapping_sub(l); } else { + let new_s = s; s = s.offset(1); *new_s = **ws as c_char; n = n.wrapping_sub(1); } + *ws = (*ws).offset(1); } + while n != 0 { - if **ws.wrapping_sub(1) >= 0x7f { + if (**ws).wrapping_sub(1) >= 0x7f { if **ws == 0 { *s = 0; *ws = 0 as *const wchar_t; @@ -366,6 +371,7 @@ pub unsafe extern "C" fn wcsrtombs( } *ws = (*ws).offset(1); } + return N; } diff --git a/tests/wchar/wcsrtombs.c b/tests/wchar/wcsrtombs.c index 0a070ca696..53bbd8044f 100644 --- a/tests/wchar/wcsrtombs.c +++ b/tests/wchar/wcsrtombs.c @@ -7,11 +7,11 @@ int main() { const wchar_t *wcs1 = L""; size_t mb_len1 = wcsrtombs(NULL, &wcs1, 0, NULL); assert(mb_len1 == 0); - + const wchar_t *wcs2 = L"Hello, world!"; size_t mb_len2 = wcsrtombs(NULL, &wcs2, 0, NULL); assert(mb_len2 == strlen("Hello, world!")); - + char *mbs2 = malloc(mb_len2 + 1); wcsrtombs(mbs2, &wcs2, mb_len2 + 1, NULL); assert(strcmp(mbs2, "Hello, world!") == 0);