Skip to content

fix: accept an empty gzip as a valid input #349

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions src/codec/gzip/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{
},
util::PartialBuffer,
};
use std::io::{Error, ErrorKind, Result};

use flate2::Crc;
use std::io::{Error, ErrorKind, Read, Result};
use std::ops::Deref;

#[derive(Debug)]
enum State {
Expand Down Expand Up @@ -163,16 +163,16 @@ impl Decode for GzipDecoder {

fn finish(
&mut self,
_output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
// Because of the footer we have to have already flushed all the data out before we get here
if let State::Done = self.state {
Ok(true)
} else {
Err(Error::new(
match &mut self.state {
State::Done => Ok(true),
// In this case, the input was an empty gzip. Exit successfully with an empty gzip.
State::Header(parser) if parser.has_no_content() => Ok(true),
_ => Err(Error::new(
ErrorKind::UnexpectedEof,
"unexpected end of file",
))
)),
}
}
}
4 changes: 4 additions & 0 deletions src/codec/gzip/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,8 @@ impl Parser {
};
}
}

pub(super) fn has_no_content(&self) -> bool {
matches!(&self.state, State::Fixed(data) if data.buffer().iter().all(|&b| b == 0))
}
}
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ impl<B: AsRef<[u8]>> PartialBuffer<B> {
&self.buffer.as_ref()[self.index..]
}

pub(crate) fn buffer(&self) -> &[u8] {
self.buffer.as_ref()
}

pub(crate) fn advance(&mut self, amount: usize) {
self.index += amount;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ test_cases!(gzip);
#[allow(unused)]
use utils::{algos::gzip::sync, InputStream};

#[allow(unused)]
use ntest::assert_true;

#[cfg(feature = "futures-io")]
use utils::algos::gzip::futures::bufread;

Expand Down Expand Up @@ -51,3 +54,28 @@ fn gzip_bufread_chunks_decompress_with_extra_header() {

assert_eq!(output, &[1, 2, 3, 4, 5, 6][..]);
}

#[test]
#[ntest::timeout(1000)]
#[cfg(feature = "futures-io")]
fn gzip_empty() {
let bytes = Vec::new();

let input = InputStream::from(bytes.chunks(2));
let output = bufread::decompress(bufread::from(&input));

assert_eq!(output, &[][..]);
}

#[test]
#[ntest::timeout(1000)]
#[cfg(feature = "futures-io")]
fn invalid_gzip() {
let bytes = [0, 0, 0, 1, 0, 0];

let input = InputStream::from(bytes.chunks(2));

let result = std::panic::catch_unwind(|| bufread::decompress(bufread::from(&input)));

assert_true!(result.is_err());
}