Skip to content

Commit 0d8fd23

Browse files
committed
implement review suggestions
1 parent bd8e088 commit 0d8fd23

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

library/std/src/io/buffered/bufreader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<R: Read> BufRead for BufReader<R> {
380380

381381
let mut readbuf = ReadBuf::uninit(&mut self.buf);
382382

383-
// SAFETY: `self.init` is either 0 set to `readbuf.initialized_len()`
383+
// SAFETY: `self.init` is either 0 or set to `readbuf.initialized_len()`
384384
// from the last time this function was called
385385
unsafe {
386386
readbuf.assume_init(self.init);

library/std/src/io/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,9 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
371371
}
372372

373373
let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
374+
375+
// SAFETY: These bytes were initalized but not filled in the previous loop
374376
unsafe {
375-
// add back extra initalized bytes, we don't want to reinitalize initalized bytes
376377
read_buf.assume_init(initialized);
377378
}
378379

@@ -389,6 +390,8 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
389390
// store how much was initialized but not filled
390391
initialized = read_buf.initialized_len() - read_buf.filled_len();
391392
let new_len = read_buf.filled_len() + buf.len();
393+
394+
// SAFETY: ReadBuf's invariants mean this much memory is init
392395
unsafe {
393396
buf.set_len(new_len);
394397
}
@@ -2558,11 +2561,17 @@ impl<T: Read> Read for Take<T> {
25582561
let prev_filled = buf.filled_len();
25592562

25602563
if self.limit <= buf.remaining() as u64 {
2561-
let extra_init = buf.initialized_len() - buf.filled_len();
2562-
let ibuf = unsafe { &mut buf.unfilled_mut()[..self.limit as usize] };
2564+
// if we just use an as cast to convert, limit may wrap around on a 32 bit target
2565+
let limit = cmp::min(self.limit, usize::MAX as u64) as usize;
2566+
2567+
let extra_init = cmp::min(limit as usize, buf.initialized_len() - buf.filled_len());
2568+
2569+
// SAFETY: no uninit data is written to ibuf
2570+
let ibuf = unsafe { &mut buf.unfilled_mut()[..limit] };
25632571

25642572
let mut sliced_buf = ReadBuf::uninit(ibuf);
25652573

2574+
// SAFETY: extra_init bytes of ibuf are known to be initialized
25662575
unsafe {
25672576
sliced_buf.assume_init(extra_init);
25682577
}
@@ -2574,6 +2583,7 @@ impl<T: Read> Read for Take<T> {
25742583

25752584
// sliced_buf / ibuf must drop here
25762585

2586+
// SAFETY: new_init bytes of buf's unfilled buffer have been initialized
25772587
unsafe {
25782588
buf.assume_init(new_init);
25792589
}

library/std/src/io/readbuf.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a> ReadBuf<'a> {
4343
let len = buf.len();
4444

4545
ReadBuf {
46-
//SAFETY: inintialized data never becoming uninitialized is an invariant of ReadBuf
46+
//SAFETY: initialized data never becoming uninitialized is an invariant of ReadBuf
4747
buf: unsafe { (buf as *mut [u8]).as_uninit_slice_mut().unwrap() },
4848
filled: 0,
4949
initialized: len,
@@ -135,10 +135,10 @@ impl<'a> ReadBuf<'a> {
135135
pub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8] {
136136
assert!(self.remaining() >= n);
137137

138-
//dont try to do any zeroing if we already have enough initialized
139-
if n > (self.initialized - self.filled) {
140-
let uninit = (n + self.filled) - self.initialized;
141-
138+
let extra_init = self.initialized - self.filled;
139+
// If we dont have enough initialized, do zeroing
140+
if n > extra_init {
141+
let uninit = n - extra_init;
142142
let unfilled = &mut self.uninitialized_mut()[0..uninit];
143143

144144
for byte in unfilled.iter_mut() {

library/std/src/io/readbuf/tests.rs

+12
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,15 @@ fn append() {
167167
assert_eq!(rbuf.filled_len(), 16);
168168
assert_eq!(rbuf.filled(), [1; 16]);
169169
}
170+
171+
#[test]
172+
fn filled_mut() {
173+
let mut buf = [0; 16];
174+
let mut rbuf = ReadBuf::new(&mut buf);
175+
176+
rbuf.add_filled(8);
177+
178+
let filled = rbuf.filled().to_vec();
179+
180+
assert_eq!(&*filled, &*rbuf.filled_mut());
181+
}

0 commit comments

Comments
 (0)