Fixing wcsrtombs

This commit is contained in:
Darley Barreto
2023-05-10 12:48:33 +00:00
committed by Jeremy Soller
parent b25bbae2e0
commit 82eb083091
2 changed files with 10 additions and 4 deletions
+8 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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);