From b07c1f7f4d46f083725a03f85a3c6cd3447267ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Wed, 13 Mar 2024 15:09:39 +0100 Subject: [PATCH 1/3] Improve several `Read` implementations --- library/std/src/io/cursor.rs | 21 +++++++++++++++++++++ library/std/src/io/impls.rs | 4 +++- library/std/src/process.rs | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index 49dde828c1f..c4e97fc45ff 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -364,6 +364,27 @@ where self.pos += n as u64; Ok(()) } + + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + let content = self.remaining_slice(); + let len = content.len(); + buf.try_reserve(len)?; + buf.extend_from_slice(content); + self.pos += len as u64; + + Ok(len) + } + + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + let content = + crate::str::from_utf8(self.remaining_slice()).map_err(|_| io::Error::INVALID_UTF8)?; + let len = content.len(); + buf.try_reserve(len)?; + buf.push_str(content); + self.pos += len as u64; + + Ok(len) + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index dd7e0725176..9f9ee4af5c8 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -329,8 +329,9 @@ impl Read for &[u8] { #[inline] fn read_to_string(&mut self, buf: &mut String) -> io::Result { let content = str::from_utf8(self).map_err(|_| io::Error::INVALID_UTF8)?; - buf.push_str(content); let len = self.len(); + buf.try_reserve(len)?; + buf.push_str(content); *self = &self[len..]; Ok(len) } @@ -478,6 +479,7 @@ impl Read for VecDeque { let len = self.len(); let content = self.make_contiguous(); let string = str::from_utf8(content).map_err(|_| io::Error::INVALID_UTF8)?; + buf.try_reserve(len)?; buf.push_str(string); self.clear(); Ok(len) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 69cc61b30ef..ab282135d1a 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -486,6 +486,10 @@ impl Read for ChildStderr { fn is_read_vectored(&self) -> bool { self.inner.is_read_vectored() } + + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + self.inner.read_to_end(buf) + } } impl AsInner for ChildStderr { From 23211b638a78bc7b3e69b8e8cdd25abebeb0f7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Thu, 14 Mar 2024 11:20:59 +0100 Subject: [PATCH 2/3] `VecDeque::read_to_string`: avoid making the slices contiguous --- library/std/src/io/impls.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index 9f9ee4af5c8..46f04c7cd39 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -474,15 +474,8 @@ impl Read for VecDeque { #[inline] fn read_to_string(&mut self, buf: &mut String) -> io::Result { - // We have to use a single contiguous slice because the `VecDequeue` might be split in the - // middle of an UTF-8 character. - let len = self.len(); - let content = self.make_contiguous(); - let string = str::from_utf8(content).map_err(|_| io::Error::INVALID_UTF8)?; - buf.try_reserve(len)?; - buf.push_str(string); - self.clear(); - Ok(len) + // SAFETY: We only append to the buffer + unsafe { io::append_to_string(buf, |buf| self.read_to_end(buf)) } } } From 2e3ee230220cc81b159d99d833cba46c667e3321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Wed, 10 Apr 2024 19:16:31 +0200 Subject: [PATCH 3/3] Avoid panicking branch in `append_to_string` --- library/std/src/io/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 98973a43e1d..450fa43ebea 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -384,7 +384,10 @@ where { let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() }; let ret = f(g.buf); - if str::from_utf8(&g.buf[g.len..]).is_err() { + + // SAFETY: the caller promises to only append data to `buf` + let appended = g.buf.get_unchecked(g.len..); + if str::from_utf8(appended).is_err() { ret.and_then(|_| Err(Error::INVALID_UTF8)) } else { g.len = g.buf.len();