Skip to content

Commit dece038

Browse files
jongiddyfolkertdev
authored andcommitted
Fix infinite loop after invalid data
Closes #98
1 parent 090227a commit dece038

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/write.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,17 @@ impl<W: Write> Write for BzDecoder<W> {
254254
let res = self.data.decompress_vec(data, &mut self.buf);
255255
let written = (self.total_in() - before) as usize;
256256

257-
let res = res.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
258-
259-
if res == Status::StreamEnd {
260-
self.done = true;
257+
match res {
258+
Err(e) => {
259+
self.done = true;
260+
return Err(io::Error::new(io::ErrorKind::InvalidInput, e));
261+
}
262+
Ok(Status::StreamEnd) => {
263+
self.done = true;
264+
}
265+
Ok(_) => {}
261266
}
267+
262268
if written > 0 || data.is_empty() || self.done {
263269
return Ok(written);
264270
}
@@ -309,6 +315,14 @@ mod tests {
309315
assert_eq!(&data[..], b"");
310316
}
311317

318+
// https://github.com/alexcrichton/bzip2-rs/issues/98
319+
#[test]
320+
fn write_invalid() {
321+
let mut d = BzDecoder::new(Vec::new());
322+
let e = d.write(b"BZh\xfb").unwrap_err();
323+
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
324+
}
325+
312326
#[test]
313327
fn qc() {
314328
::quickcheck::quickcheck(test as fn(_) -> _);

0 commit comments

Comments
 (0)