Skip to content

Commit d2dc330

Browse files
committed
BufWriter: Note non-obvious safety assumption in BorrowedBuf::set_init usage.
CC #78485, #117693.
1 parent 12f35ad commit d2dc330

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

library/std/src/io/copy.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,11 @@ impl<I: Write + ?Sized> BufferedWriterSpec for BufWriter<I> {
219219
loop {
220220
let buf = self.buffer_mut();
221221
let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
222-
223222
if init {
224-
// SAFETY: init is either 0 or the init_len from the previous iteration.
223+
// SAFETY: `init` is only true after `reader` initializes `read_buf`.
224+
// `flush_buf` won't cause any part of the spare capacity to become uninitialized or
225+
// cause `self.buf` to reallocate, so it is OK to persist this across `flush_buf`
226+
// calls. FIXME: This seems like a dangerous assumption.
225227
unsafe { read_buf.set_init() };
226228
}
227229

0 commit comments

Comments
 (0)