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

Fix infinite loop after invalid data #99

Merged
Merged
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
16 changes: 13 additions & 3 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,13 @@ impl<W: Write> Write for BzDecoder<W> {
let res = self.data.decompress_vec(data, &mut self.buf);
let written = (self.total_in() - before) as usize;

let res = res.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
// make sure that a subsequent call exits early when there is nothing useful left to do
self.done |= matches!(res, Err(_) | Ok(Status::StreamEnd));

if res == Status::StreamEnd {
self.done = true;
if let Err(e) = res {
return Err(io::Error::new(io::ErrorKind::InvalidInput, e));
}

if written > 0 || data.is_empty() || self.done {
return Ok(written);
}
Expand Down Expand Up @@ -309,6 +311,14 @@ mod tests {
assert_eq!(&data[..], b"");
}

#[test]
fn write_invalid() {
// see https://github.com/trifectatechfoundation/bzip2-rs/issues/98
let mut d = BzDecoder::new(Vec::new());
let e = d.write(b"BZh\xfb").unwrap_err();
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
}

#[test]
fn qc() {
::quickcheck::quickcheck(test as fn(_) -> _);
Expand Down
Loading