Skip to content

Commit 953f849

Browse files
authored
Rollup merge of #112154 - ShaneEverittM:zero-len-utf16, r=thomcc
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.
2 parents ae106cf + 1293c17 commit 953f849

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use crate::sys::cvt;
1111
use crate::sys::handle::Handle;
1212
use core::str::utf8_char_width;
1313

14+
#[cfg(test)]
15+
mod tests;
16+
1417
// Don't cache handles but get them fresh for every read/write. This allows us to track changes to
1518
// the value over time (such as if a process calls `SetStdHandle` while it's running). See #40490.
1619
pub struct Stdin {
@@ -383,6 +386,10 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
383386
debug_assert!(utf16.len() <= c::c_int::MAX as usize);
384387
debug_assert!(utf8.len() <= c::c_int::MAX as usize);
385388

389+
if utf16.is_empty() {
390+
return Ok(0);
391+
}
392+
386393
let result = unsafe {
387394
c::WideCharToMultiByte(
388395
c::CP_UTF8, // CodePage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use super::utf16_to_utf8;
2+
3+
#[test]
4+
fn zero_size_read() {
5+
assert_eq!(utf16_to_utf8(&[], &mut []).unwrap(), 0);
6+
}

0 commit comments

Comments
 (0)