From d11469404274c7162b295105846f98111bc273ab Mon Sep 17 00:00:00 2001 From: Count Count Date: Wed, 24 Mar 2021 07:05:17 +0100 Subject: [PATCH] Reject byte if it cannot start a valid UTF-8 sequence. --- library/std/src/sys/windows/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/windows/stdio.rs b/library/std/src/sys/windows/stdio.rs index 0812fa51f07a..19fd900580b3 100644 --- a/library/std/src/sys/windows/stdio.rs +++ b/library/std/src/sys/windows/stdio.rs @@ -128,7 +128,8 @@ fn write( let utf8 = match str::from_utf8(&data[..len]) { Ok(s) => s, Err(ref e) if e.valid_up_to() == 0 => { - if data.len() < utf8_char_width(data[0]) { + first_byte_char_width = utf8_char_width(data[0]); + if first_byte_char_width > 1 && data.len() < first_byte_char_width { incomplete_utf8.bytes[0] = data[0]; incomplete_utf8.len = 1; return Ok(1);