Prevent seek before offset

This commit is contained in:
Roderick van Domburg 2022-01-13 19:12:48 +01:00
parent 8d8d6d4fd8
commit 78216eb6ee
No known key found for this signature in database
GPG key ID: A9EF5222A26F0451

View file

@ -2195,19 +2195,20 @@ impl<T: Read + Seek> Seek for Subfile<T> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
let pos = match pos { let pos = match pos {
SeekFrom::Start(offset) => SeekFrom::Start(offset + self.offset), SeekFrom::Start(offset) => SeekFrom::Start(offset + self.offset),
x => x, SeekFrom::End(offset) => {
if (self.length as i64 - offset) < self.offset as i64 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"newpos would be < self.offset",
));
}
pos
}
_ => pos,
}; };
let newpos = self.stream.seek(pos)?; let newpos = self.stream.seek(pos)?;
Ok(newpos - self.offset)
if newpos >= self.offset {
Ok(newpos - self.offset)
} else {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"newpos < self.offset",
))
}
} }
} }