Skip to content

Commit

Permalink
Fix infinite loop after invalid data
Browse files Browse the repository at this point in the history
  • Loading branch information
jongiddy authored and bjorn3 committed Dec 11, 2024
1 parent 090227a commit 9fbe6a9
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,17 @@ 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))?;

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

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

// https://github.com/alexcrichton/bzip2-rs/issues/98
#[test]
fn write_invalid() {
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

0 comments on commit 9fbe6a9

Please sign in to comment.