diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-06-01 22:47:32 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-01 22:47:32 +0200 |
| commit | a66ba9b10b78063e4bd6afbc601786eed3159ade (patch) | |
| tree | c8df5b72818d0b01bb8ae3f33a0d18f6a5e5de0e /library | |
| parent | 20b25d233a9efd9d8dd39d5c506d70fe73c3e16b (diff) | |
| parent | 1293c1720574144ec13df2bbdcf26dab8e3b771f (diff) | |
| download | rust-a66ba9b10b78063e4bd6afbc601786eed3159ade.tar.gz rust-a66ba9b10b78063e4bd6afbc601786eed3159ade.zip | |
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.
Diffstat (limited to 'library')
| -rw-r--r-- | library/std/src/sys/windows/stdio.rs | 7 | ||||
| -rw-r--r-- | library/std/src/sys/windows/stdio/tests.rs | 6 |
2 files changed, 13 insertions, 0 deletions
diff --git a/library/std/src/sys/windows/stdio.rs b/library/std/src/sys/windows/stdio.rs index 2e3e0859dc1..3fcaaa508e3 100644 --- a/library/std/src/sys/windows/stdio.rs +++ b/library/std/src/sys/windows/stdio.rs @@ -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 diff --git a/library/std/src/sys/windows/stdio/tests.rs b/library/std/src/sys/windows/stdio/tests.rs new file mode 100644 index 00000000000..1e53e0bee63 --- /dev/null +++ b/library/std/src/sys/windows/stdio/tests.rs @@ -0,0 +1,6 @@ +use super::utf16_to_utf8; + +#[test] +fn zero_size_read() { + assert_eq!(utf16_to_utf8(&[], &mut []).unwrap(), 0); +} |
