Skip to content
Merged
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
69 changes: 69 additions & 0 deletions test-libz-rs-sys/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,75 @@ fn deflate_bound_gzip_header_help(
true
}

#[test]
fn gz_header_text_and_hcrc_check() {
// when constructing the gz header flags, zlib-rs performed a check
// for `text > 0` that should have been `text != 0`, similarly for hcrc.
let config = DeflateConfig {
level: 9,
method: Method::Deflated,
window_bits: 31,
mem_level: 3,
strategy: Strategy::Fixed,
};

assert_eq_rs_ng!({
let mut strm = MaybeUninit::zeroed();

// first validate the config
let err = deflateInit2_(
strm.as_mut_ptr(),
config.level,
config.method as i32,
config.window_bits,
config.mem_level,
config.strategy as i32,
zlibVersion(),
core::mem::size_of::<z_stream>() as _,
);

assert_eq!(err, 0);

let strm = strm.assume_init_mut();

let mut header = gz_header {
text: -42,
time: 0,
os: 0,
extra: core::ptr::null_mut(),
extra_len: 0,
name: [0u8].as_mut_ptr(),
comment: [0u8].as_mut_ptr(),
hcrc: -42,
//
xflags: 0,
extra_max: 0,
name_max: 0,
comm_max: 0,
done: 0,
};

// this may fail if the config is not set up for gzip
let _ = deflateSetHeader(strm, &mut header);

let input = "\0\u{2}\0\0\0\0\0\0\0\u{10}";
let mut output = [0u8; 32];

strm.avail_in = input.len() as _;
strm.avail_out = output.len() as _;

strm.next_in = input.as_ptr().cast_mut();
strm.next_out = output.as_mut_ptr();

let err = deflate(strm, DeflateFlush::NoFlush as i32);
assert_eq!(err, 0);

deflateEnd(strm);

output
});
}

#[test]
#[cfg_attr(miri, ignore = "slow")]
fn deflate_bound_gzip_header() {
Expand Down
4 changes: 2 additions & 2 deletions zlib-rs/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ impl gz_header {
};

pub(crate) fn flags(&self) -> u8 {
(if self.text > 0 { 1 } else { 0 })
+ (if self.hcrc > 0 { 2 } else { 0 })
(if self.text != 0 { 1 } else { 0 })
+ (if self.hcrc != 0 { 2 } else { 0 })
+ (if self.extra.is_null() { 0 } else { 4 })
+ (if self.name.is_null() { 0 } else { 8 })
+ (if self.comment.is_null() { 0 } else { 16 })
Expand Down
2 changes: 1 addition & 1 deletion zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,7 @@ pub fn deflate(stream: &mut DeflateStream, flush: DeflateFlush) -> ReturnCode {
stream.state.bit_writer.pending.extend(&bytes);
}

if gzhead.hcrc > 0 {
if gzhead.hcrc != 0 {
stream.adler = crc32(
stream.adler as u32,
stream.state.bit_writer.pending.pending(),
Expand Down