about summary refs log tree commit diff
path: root/library/std/src/io/mod.rs
diff options
context:
space:
mode:
authorBenoît du Garreau <bdgdlm@outlook.com>2024-02-07 16:21:16 +0100
committerBenoît du Garreau <bdgdlm@outlook.com>2024-02-07 16:46:28 +0100
commit0a42a540c603846aa22f29f378a61a64c9d4383e (patch)
treedad404fa90ea313e7dd072af4b62a44a568e59ee /library/std/src/io/mod.rs
parent0809f78c190eb9fdf36353d423147827610f33c9 (diff)
downloadrust-0a42a540c603846aa22f29f378a61a64c9d4383e.tar.gz
rust-0a42a540c603846aa22f29f378a61a64c9d4383e.zip
Make `io::BorrowedCursor::advance` safe
This also keeps the old `advance` method under `advance_unchecked` name.

This makes pattern like `std::io::default_read_buf` safe to write.
Diffstat (limited to 'library/std/src/io/mod.rs')
-rw-r--r--library/std/src/io/mod.rs12
1 files changed, 2 insertions, 10 deletions
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
index a238e74ed95..f842a0b6d55 100644
--- a/library/std/src/io/mod.rs
+++ b/library/std/src/io/mod.rs
@@ -578,15 +578,7 @@ where
     F: FnOnce(&mut [u8]) -> Result<usize>,
 {
     let n = read(cursor.ensure_init().init_mut())?;
-    assert!(
-        n <= cursor.capacity(),
-        "read should not return more bytes than there is capacity for in the read buffer"
-    );
-    unsafe {
-        // SAFETY: we initialised using `ensure_init` so there is no uninit data to advance to
-        // and we have checked that the read amount is not over capacity (see #120603)
-        cursor.advance(n);
-    }
+    cursor.advance(n);
     Ok(())
 }
 
@@ -2915,7 +2907,7 @@ impl<T: Read> Read for Take<T> {
 
             unsafe {
                 // SAFETY: filled bytes have been filled and therefore initialized
-                buf.advance(filled);
+                buf.advance_unchecked(filled);
                 // SAFETY: new_init bytes of buf's unfilled buffer have been initialized
                 buf.set_init(new_init);
             }