Skip to content

Commit

Permalink
Ensure EOF is returned when gz finishes
Browse files Browse the repository at this point in the history
Previously we'd return an I/O error after the first EOF was returned as we'd try
to verify the checksum again, and this commit now sets a flag to indicate that
the checksum has already been verified so we can always return EOF after the
first one is seen.

Closes rust-lang#25
  • Loading branch information
alexcrichton committed Aug 31, 2015
1 parent 8e36637 commit 75b0f9d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/gz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct Builder {
pub struct DecoderReader<R: Read> {
inner: CrcReader<raw::DecoderReader<R>>,
header: Header,
finished: bool,
}

/// A structure representing the header of a gzip stream.
Expand Down Expand Up @@ -383,7 +384,8 @@ impl<R: Read> DecoderReader<R> {
filename: filename,
comment: comment,
mtime: mtime,
}
},
finished: false,
});

fn bad_header() -> io::Error {
Expand Down Expand Up @@ -411,6 +413,9 @@ impl<R: Read> DecoderReader<R> {
pub fn header(&self) -> &Header { &self.header }

fn finish(&mut self) -> io::Result<()> {
if self.finished {
return Ok(())
}
let ref mut buf = [0u8; 8];
{
let mut len = 0;
Expand All @@ -433,6 +438,7 @@ impl<R: Read> DecoderReader<R> {
((buf[7] as u32) << 24);
if crc != self.inner.crc().sum() as u32 { return Err(corrupt()) }
if amt != self.inner.crc().amt() { return Err(corrupt()) }
self.finished = true;
Ok(())
}
}
Expand Down Expand Up @@ -539,4 +545,17 @@ mod tests {
assert_eq!(res, vec![0, 2, 4, 6]);

}

#[test]
fn keep_reading_after_end() {
let mut e = EncoderWriter::new(Vec::new(), Default);
e.write_all(b"foo bar baz").unwrap();
let inner = e.finish().unwrap();
let mut d = DecoderReader::new(&inner[..]).unwrap();
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
assert_eq!(s, "foo bar baz");
d.read_to_string(&mut s).unwrap();
assert_eq!(s, "foo bar baz");
}
}

0 comments on commit 75b0f9d

Please sign in to comment.