Skip to content

Commit

Permalink
Fix io::default_read_to_end uses of read_buf
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed May 22, 2024
1 parent 976ef15 commit 50196d3
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,14 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
}

let mut cursor = read_buf.unfilled();
loop {
let result = loop {
match r.read_buf(cursor.reborrow()) {
Ok(()) => break,
Err(e) if e.is_interrupted() => continue,
Err(e) => return Err(e),
// Do not stop now in case of error: we might have received both data
// and an error
res => break res,
}
}
};

let unfilled_but_initialized = cursor.init_ref().len();
let bytes_read = cursor.written();
Expand All @@ -493,15 +494,18 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
return Ok(buf.len() - start_len);
}

// store how much was initialized but not filled
initialized = unfilled_but_initialized;

// SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
unsafe {
let new_len = bytes_read + buf.len();
buf.set_len(new_len);
}

// Now that all data is pushed to the vector, we can fail without data loss
result?;

// store how much was initialized but not filled
initialized = unfilled_but_initialized;

// Use heuristics to determine the max read size if no initial size hint was provided
if size_hint.is_none() {
// The reader is returning short reads but it doesn't call ensure_init().
Expand Down

0 comments on commit 50196d3

Please sign in to comment.