about summary refs log tree commit diff
path: root/src/libstd/sys/unix/stack_overflow.rs
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-20 16:26:34 +0200
committerGitHub <noreply@github.com>2019-08-20 16:26:34 +0200
commita5299dd5d1c517b8a2da463ec463abec68b08b33 (patch)
tree05479e6089d60e97f2536b8571aca6db4ae1c0cf /src/libstd/sys/unix/stack_overflow.rs
parent14890954ce17c44d944eda988c5a64bb4c5ec9eb (diff)
parentedb5214b29cd7de06dd10f673986d38e568b077c (diff)
downloadrust-a5299dd5d1c517b8a2da463ec463abec68b08b33.tar.gz
rust-a5299dd5d1c517b8a2da463ec463abec68b08b33.zip
Rollup merge of #63216 - oconnor663:take_read_to_end, r=sfackler
avoid unnecessary reservations in std::io::Take::read_to_end

Prevously the `read_to_end` implementation for `std::io::Take` used its
own `limit` as a cap on the `reservation_size`. However, that could
still result in an over-allocation like this:

1. Call `reader.take(5).read_to_end(&mut vec)`.
2. `read_to_end_with_reservation` reserves 5 bytes and calls `read`.
3. `read` writes 5 bytes.
4. `read_to_end_with_reservation` reserves 5 bytes and calls `read`.
5. `read` writes 0 bytes.
6. The read loop ends with `vec` having length 5 and capacity 10.

The reservation of 5 bytes was correct for the read at step 2 but
unnecessary for the read at step 4. By that second read, `Take::limit`
is 0, but the `read_to_end_with_reservation` loop is still using the
same `reservation_size` it started with.

Solve this by having `read_to_end_with_reservation` take a closure,
which lets it get a fresh `reservation_size` for each read. This is an
implementation detail which doesn't affect any public API.
Diffstat (limited to 'src/libstd/sys/unix/stack_overflow.rs')
0 files changed, 0 insertions, 0 deletions