From 4bee5f5836d93ac9211b5ea574d10c3982f00e83 Mon Sep 17 00:00:00 2001 From: Olivier ROLAND Date: Tue, 24 Oct 2023 18:49:03 +0200 Subject: [PATCH 1/2] Fix infinite loop caused by invalid UTF-8 bytes --- futures-util/src/io/read_line.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/futures-util/src/io/read_line.rs b/futures-util/src/io/read_line.rs index e1b8fc9455..df782c9570 100644 --- a/futures-util/src/io/read_line.rs +++ b/futures-util/src/io/read_line.rs @@ -35,6 +35,7 @@ pub(super) fn read_line_internal( ) -> Poll> { let ret = ready!(read_until_internal(reader, cx, b'\n', bytes, read)); if str::from_utf8(bytes).is_err() { + bytes.clear(); Poll::Ready(ret.and_then(|_| { Err(io::Error::new(io::ErrorKind::InvalidData, "stream did not contain valid UTF-8")) })) From 92fdd59c295dbad09dd6ee0bf5eed5b16b33a7a2 Mon Sep 17 00:00:00 2001 From: Olivier ROLAND Date: Wed, 25 Oct 2023 09:30:45 +0200 Subject: [PATCH 2/2] Extend io::AsyncBufReadExt::lines example with invalid UTF-8 --- futures-util/src/io/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/futures-util/src/io/mod.rs b/futures-util/src/io/mod.rs index 4f474f757d..ff430d76b1 100644 --- a/futures-util/src/io/mod.rs +++ b/futures-util/src/io/mod.rs @@ -807,11 +807,11 @@ pub trait AsyncBufReadExt: AsyncBufRead { /// use futures::io::{AsyncBufReadExt, Cursor}; /// use futures::stream::StreamExt; /// - /// let cursor = Cursor::new(b"lorem\nipsum\r\ndolor"); + /// let cursor = Cursor::new(b"lorem\nipsum\xc2\r\ndolor"); /// - /// let mut lines_stream = cursor.lines().map(|l| l.unwrap()); + /// let mut lines_stream = cursor.lines().map(|l| l.unwrap_or(String::from("invalid UTF_8"))); /// assert_eq!(lines_stream.next().await, Some(String::from("lorem"))); - /// assert_eq!(lines_stream.next().await, Some(String::from("ipsum"))); + /// assert_eq!(lines_stream.next().await, Some(String::from("invalid UTF_8"))); /// assert_eq!(lines_stream.next().await, Some(String::from("dolor"))); /// assert_eq!(lines_stream.next().await, None); /// # Ok::<(), Box>(()) }).unwrap();