Skip to content

Fix bug in utf16_to_utf8 for zero length strings #112154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library/std/src/sys/windows/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/windows/stdio/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::utf16_to_utf8;

#[test]
fn zero_size_read() {
assert_eq!(utf16_to_utf8(&[], &mut []).unwrap(), 0);
}