From 4abc28f57013bd3a3d17c858fca660ca62daeb15 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 22 Apr 2026 10:51:13 -0700 Subject: [PATCH 1/4] `::read_buf`: Clarify local variable name. --- library/std/src/io/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 1166ba8baf43..54b1742c6f25 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -3084,7 +3084,6 @@ fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> { // SAFETY: no uninit data is written to ibuf let ibuf = unsafe { &mut buf.as_mut()[..limit] }; - let mut sliced_buf: BorrowedBuf<'_> = ibuf.into(); if is_init { @@ -3096,14 +3095,14 @@ fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> { let mut cursor = sliced_buf.unfilled(); let result = self.inner.read_buf(cursor.reborrow()); - let should_init = cursor.is_init(); + let did_init_up_to_limit = sliced_buf.is_init(); let filled = sliced_buf.len(); // cursor / sliced_buf / ibuf must drop here // Avoid accidentally quadratic behaviour by initializing the whole // cursor if only part of it was initialized. - if should_init { + if did_init_up_to_limit { // SAFETY: no uninit data is written let uninit = unsafe { &mut buf.as_mut()[limit..] }; uninit.write_filled(0); From 71076f2338a7912eed39d8584ab5e3d58145f78a Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 22 Apr 2026 11:14:18 -0700 Subject: [PATCH 2/4] `::read_buf`: Eliminate unneeded local variables. Eliminate `cursor` and `ibuf` as named variables, as their presence makes things more confusing. --- library/std/src/io/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 54b1742c6f25..83b9667f9a57 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -3083,8 +3083,7 @@ fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> { let is_init = buf.is_init(); // SAFETY: no uninit data is written to ibuf - let ibuf = unsafe { &mut buf.as_mut()[..limit] }; - let mut sliced_buf: BorrowedBuf<'_> = ibuf.into(); + let mut sliced_buf = BorrowedBuf::from(unsafe { &mut buf.as_mut()[..limit] }); if is_init { // SAFETY: `sliced_buf` is a subslice of `buf`, so if `buf` was initialized then @@ -3092,13 +3091,12 @@ fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> { unsafe { sliced_buf.set_init() }; } - let mut cursor = sliced_buf.unfilled(); - let result = self.inner.read_buf(cursor.reborrow()); + let result = self.inner.read_buf(sliced_buf.unfilled()); let did_init_up_to_limit = sliced_buf.is_init(); let filled = sliced_buf.len(); - // cursor / sliced_buf / ibuf must drop here + // sliced_buf must drop here // Avoid accidentally quadratic behaviour by initializing the whole // cursor if only part of it was initialized. From c716ce5c2eaa741f6a3b7383a09f473df5ed5ad3 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 22 Apr 2026 11:05:35 -0700 Subject: [PATCH 3/4] `::read_buf`: Clarify safety comments and naming. --- library/std/src/io/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 83b9667f9a57..1812bf3ac927 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -3101,11 +3101,13 @@ fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> { // Avoid accidentally quadratic behaviour by initializing the whole // cursor if only part of it was initialized. if did_init_up_to_limit { - // SAFETY: no uninit data is written - let uninit = unsafe { &mut buf.as_mut()[limit..] }; - uninit.write_filled(0); - // SAFETY: all bytes that were not initialized by `T::read_buf` - // have just been written to. + // SAFETY: No uninit data will be written. + let unfilled_before_advance = unsafe { buf.as_mut() }; + + unfilled_before_advance[limit..].write_filled(0); + + // SAFETY: `unfilled_before_advance[..limit]` was initialized by `T::read_buf`, and + // `unfilled_before_advance[limit..]` was just initialized. unsafe { buf.set_init() }; } From 3a0a14fd7c187e27eeba7dfe0f7467d18625e162 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 22 Apr 2026 11:15:27 -0700 Subject: [PATCH 4/4] `::read_buf`: Don't initialize `buf` if it was already initialized. --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 1812bf3ac927..0a644caa5016 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -3100,7 +3100,7 @@ fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> { // Avoid accidentally quadratic behaviour by initializing the whole // cursor if only part of it was initialized. - if did_init_up_to_limit { + if did_init_up_to_limit && !is_init { // SAFETY: No uninit data will be written. let unfilled_before_advance = unsafe { buf.as_mut() };