Skip to content

Ensure the read_to_end buffer always has enough room to fit a single UTF-8 code point #142872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ where
// - pass large buffers to readers that do not initialize the spare capacity. this can amortize per-call overheads
// - and finally pass not-too-small and not-too-large buffers to Windows read APIs because they manage to suffer from both problems
// at the same time, i.e. small reads suffer from syscall overhead, all reads incur costs proportional to buffer size (#110650)
//
// - also avoid <4 byte reads as this may split UTF-8 code points, which can be a problem for Windows console reads (#142847)
pub(crate) fn default_read_to_end<R: Read + ?Sized>(
r: &mut R,
buf: &mut Vec<u8>,
Expand Down Expand Up @@ -452,7 +452,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
let mut consecutive_short_reads = 0;

loop {
if buf.len() == buf.capacity() && buf.capacity() == start_cap {
if buf.spare_capacity_mut().len() < PROBE_SIZE && buf.capacity() == start_cap {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that it would be a good idea too, but small_probe_read is designed to check for EOF so taking this branch if we still have space in the vector does not seem right.

Copy link
Member Author

@ChrisDenton ChrisDenton Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be a tight loop until either EOF or it exceeds the spare space. It'll only ever get past that point once a reallocation is required.

Copy link
Contributor

@a1phyr a1phyr Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having thought a bit about that, your solution avoids an issue with small reads if the vector was preallocated.

// The buffer might be an exact fit. Let's read into a probe buffer
// and see if it returns `Ok(0)`. If so, we've avoided an
// unnecessary doubling of the capacity. But if not, append the
Expand All @@ -462,12 +462,13 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
if read == 0 {
return Ok(buf.len() - start_len);
}
// In the case of very short reads, continue to use the stack buffer
// until either we reach the end or we need to reallocate.
continue;
}

if buf.len() == buf.capacity() {
// buf is full, need more space
buf.try_reserve(PROBE_SIZE)?;
}
// Avoid unnecessarily short reads by ensuring there's at least PROBE_SIZE space available.
buf.try_reserve(PROBE_SIZE)?;

let mut spare = buf.spare_capacity_mut();
let buf_len = cmp::min(spare.len(), max_read_size);
Expand Down
Loading