Skip to content

Commit

Permalink
Use checked_add_signed.
Browse files Browse the repository at this point in the history
This will result in errors being emitted in a few cases where seek requests are
too big or too weird.

This is a first step to diagnosing #74, though it seems unlikely to be related.
  • Loading branch information
adetaylor committed Sep 28, 2024
1 parent d6eab53 commit 63dba36
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/unzip/cloneable_seekable_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ impl<R: Read + Seek + HasLength> Read for CloneableSeekableReader<R> {
let mut inner = self.inner.lock().unwrap();
let read_result = inner.read_at(self.pos, buf);
if let Ok(bytes_read) = read_result {
// TODO, once stabilised, use checked_add_signed
self.pos += bytes_read as u64;
self.pos = self
.pos
.checked_add(bytes_read as u64)
.ok_or(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Read too far forward",
))?;
}
read_result
}
Expand All @@ -121,17 +126,20 @@ impl<R: Read + Seek + HasLength> Seek for CloneableSeekableReader<R> {
"Seek too far backwards",
));
}
// TODO, once stabilised, use checked_add_signed
file_len - (-offset_from_end as u64)
}
// TODO, once stabilised, use checked_add_signed
SeekFrom::Current(offset_from_pos) => {
if offset_from_pos > 0 {
self.pos + (offset_from_pos as u64)
} else {
self.pos - ((-offset_from_pos) as u64)
}
file_len
.checked_add_signed(offset_from_end)
.ok_or(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek too far backward from end",
))?
}
SeekFrom::Current(offset_from_pos) => self
.pos
.checked_add_signed(offset_from_pos)
.ok_or(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek too far forward from current pos",
))?,
};
self.pos = new_pos;
Ok(new_pos)
Expand Down

0 comments on commit 63dba36

Please sign in to comment.