Skip to content
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

od: remove potential unsoundness and an allocation in PartialReader #1730

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/uu/od/src/partialreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::multifilereader::HasError;

/// When a large number of bytes must be skipped, it will be read into a
/// dynamically allocated buffer. The buffer will be limited to this size.
const MAX_SKIP_BUFFER: usize = 64 * 1024;
const MAX_SKIP_BUFFER: usize = 16 * 1024;

/// Wrapper for `std::io::Read` which can skip bytes at the beginning
/// of the input, and it can limit the returned bytes to a particular
Expand All @@ -31,21 +31,25 @@ impl<R> PartialReader<R> {
impl<R: Read> Read for PartialReader<R> {
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
if self.skip > 0 {
let buf_size = cmp::min(self.skip, MAX_SKIP_BUFFER);
let mut bytes: Vec<u8> = Vec::with_capacity(buf_size);
unsafe {
bytes.set_len(buf_size);
}
let mut bytes = [0; MAX_SKIP_BUFFER];

while self.skip > 0 {
let skip_count = cmp::min(self.skip, buf_size);

match self.inner.read_exact(&mut bytes[..skip_count]) {
let skip_count = cmp::min(self.skip, MAX_SKIP_BUFFER);

match self.inner.read(&mut bytes[..skip_count]) {
Ok(0) => {
// this is an error as we still have more to skip
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"tried to skip past end of input",
));
}
Ok(n) => self.skip -= n,
Err(e) => return Err(e),
Ok(()) => self.skip -= skip_count,
}
}
}

match self.limit {
None => self.inner.read(out),
Some(0) => Ok(0),
Expand Down