Fix bug in utf16_to_utf8 for zero length strings
This fixes the behavior of sending EOF by pressing Ctrl+Z => Enter in a windows console. Previously, that would trip the unpaired surrogate error, whereas now we correctly detect EOF.
This commit is contained in:
parent
871b595202
commit
1293c17205
@ -11,6 +11,9 @@ use crate::sys::cvt;
|
||||
use crate::sys::handle::Handle;
|
||||
use core::str::utf8_char_width;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
// Don't cache handles but get them fresh for every read/write. This allows us to track changes to
|
||||
// the value over time (such as if a process calls `SetStdHandle` while it's running). See #40490.
|
||||
pub struct Stdin {
|
||||
@ -383,6 +386,10 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
|
||||
debug_assert!(utf16.len() <= c::c_int::MAX as usize);
|
||||
debug_assert!(utf8.len() <= c::c_int::MAX as usize);
|
||||
|
||||
if utf16.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let result = unsafe {
|
||||
c::WideCharToMultiByte(
|
||||
c::CP_UTF8, // CodePage
|
||||
|
6
library/std/src/sys/windows/stdio/tests.rs
Normal file
6
library/std/src/sys/windows/stdio/tests.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use super::utf16_to_utf8;
|
||||
|
||||
#[test]
|
||||
fn zero_size_read() {
|
||||
assert_eq!(utf16_to_utf8(&[], &mut []).unwrap(), 0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user