Skip to content

Commit

Permalink
Don't infinitely return errors on invalid headers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jun 27, 2018
1 parent 9eb6555 commit 1ce113b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/gz/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<R: BufRead + Write> Write for GzEncoder<R> {
#[derive(Debug)]
pub struct GzDecoder<R> {
inner: CrcReader<deflate::bufread::DeflateDecoder<R>>,
header: io::Result<GzHeader>,
header: Option<io::Result<GzHeader>>,
finished: bool,
}

Expand All @@ -291,7 +291,7 @@ impl<R: BufRead> GzDecoder<R> {
let flate = deflate::bufread::DeflateDecoder::new(r);
GzDecoder {
inner: CrcReader::new(flate),
header: header,
header: Some(header),
finished: false,
}
}
Expand Down Expand Up @@ -330,7 +330,7 @@ impl<R: BufRead> GzDecoder<R> {
impl<R> GzDecoder<R> {
/// Returns the header associated with this stream, if it was valid
pub fn header(&self) -> Option<&GzHeader> {
self.header.as_ref().ok()
self.header.as_ref()?.as_ref().ok()
}

/// Acquires a reference to the underlying reader.
Expand All @@ -354,9 +354,15 @@ impl<R> GzDecoder<R> {

impl<R: BufRead> Read for GzDecoder<R> {
fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
if let Err(ref mut e) = self.header {
let another_error = io::ErrorKind::Other.into();
return Err(mem::replace(e, another_error));
match self.header {
None => return Ok(0), // error already returned,
Some(Ok(_)) => {}
Some(Err(_)) => {
match self.header.take().unwrap() {
Ok(_) => panic!(),
Err(e) => return Err(e)
}
}
}
if into.is_empty() {
return Ok(0);
Expand Down
10 changes: 10 additions & 0 deletions tests/gunzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ fn extract_file_multi(path_compressed: &Path) -> io::Result<Vec<u8>> {
MultiGzDecoder::new(f).read_to_end(&mut v)?;
Ok(v)
}

#[test]
fn empty_error_once() {
let data: &[u8] = &[];
let cbjson = GzDecoder::new(data);
let reader = BufReader::new(cbjson);
let mut stream = reader.lines();
assert!(stream.next().unwrap().is_err());
assert!(stream.next().is_none());
}

0 comments on commit 1ce113b

Please sign in to comment.