Skip to content

Commit 591bcb0

Browse files
committed
Only use WC_ERR_INVALID_CHARS on NT, fall back to 0 flags on incompatible versions of Windows
Fixes #18
1 parent c031dff commit 591bcb0

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

library/std/src/sys/windows/stdio.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::sys::compat;
1111
use crate::sys::cvt;
1212
use crate::sys::handle::Handle;
1313
use crate::sys::windows::api;
14+
use crate::sys::windows::api::get_last_error;
1415
use core::str::utf8_char_width;
1516

1617
#[cfg(test)]
@@ -397,18 +398,36 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
397398
return Ok(0);
398399
}
399400

400-
let result = unsafe {
401+
let is_nt = compat::is_windows_nt();
402+
403+
let mut result = unsafe {
401404
c::WideCharToMultiByte(
402-
c::CP_UTF8, // CodePage
403-
c::WC_ERR_INVALID_CHARS, // dwFlags
404-
utf16.as_ptr(), // lpWideCharStr
405-
utf16.len() as c::c_int, // cchWideChar
406-
utf8.as_mut_ptr(), // lpMultiByteStr
407-
utf8.len() as c::c_int, // cbMultiByte
408-
ptr::null(), // lpDefaultChar
409-
ptr::null_mut(), // lpUsedDefaultChar
405+
c::CP_UTF8, // CodePage
406+
if is_nt { c::WC_ERR_INVALID_CHARS } else { 0 }, // dwFlags
407+
utf16.as_ptr(), // lpWideCharStr
408+
utf16.len() as c::c_int, // cchWideChar
409+
utf8.as_mut_ptr(), // lpMultiByteStr
410+
utf8.len() as c::c_int, // cbMultiByte
411+
ptr::null(), // lpDefaultChar
412+
ptr::null_mut(), // lpUsedDefaultChar
410413
)
411414
};
415+
416+
if result == 0 && get_last_error().code == c::ERROR_INVALID_FLAGS && is_nt {
417+
result = unsafe {
418+
c::WideCharToMultiByte(
419+
c::CP_UTF8, // CodePage
420+
0, // dwFlags (0 for pre-Vista compatibility)
421+
utf16.as_ptr(), // lpWideCharStr
422+
utf16.len() as c::c_int, // cchWideChar
423+
utf8.as_mut_ptr(), // lpMultiByteStr
424+
utf8.len() as c::c_int, // cbMultiByte
425+
ptr::null(), // lpDefaultChar
426+
ptr::null_mut(), // lpUsedDefaultChar
427+
)
428+
};
429+
}
430+
412431
if result == 0 {
413432
// We can't really do any better than forget all data and return an error.
414433
Err(io::const_io_error!(

0 commit comments

Comments
 (0)