Skip to content

Commit

Permalink
fix(deflate): compress_to_vec infinite loop
Browse files Browse the repository at this point in the history
Fixed bug causing an infinite loop when compress_to_vec was called with empty or 1-length input.

Closes #75
  • Loading branch information
oyvindln committed Apr 11, 2020
1 parent 5129758 commit f3299c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion miniz_oxide/src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn compress_to_vec_inner(input: &[u8], level: u8, window_bits: i32, strategy: i3
// The comp flags function sets the zlib flag if the window_bits parameter is > 0.
let flags = create_comp_flags_from_zip_params(level.into(), window_bits, strategy);
let mut compressor = CompressorOxide::new(flags);
let mut output = vec![0; input.len() / 2];
let mut output = vec![0; std::cmp::max(input.len() / 2, 2)];

let mut in_pos = 0;
let mut out_pos = 0;
Expand Down
13 changes: 13 additions & 0 deletions miniz_oxide/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,16 @@ fn need_more_input_has_more_output_at_same_time() {
decomp(&input[..11727]); // Fail: NeedsMoreInput even if the output buffer is also full!
decomp(&input[..11728]); // Fail: NeedsMoreInput even if the output buffer is also full!
}

#[test]
fn issue_75_empty_input_infinite_loop() {
// Make sure compression works with empty input,
// a bug resulted in this causing an infinite loop in
// compress_to_vec_inner.
let c = miniz_oxide::deflate::compress_to_vec(&[], 6);
let d = miniz_oxide::inflate::decompress_to_vec(&c).expect("decompression failed!");
assert_eq!(d.len(), 0);
let c = miniz_oxide::deflate::compress_to_vec(&[0], 6);
let d = miniz_oxide::inflate::decompress_to_vec(&c).expect("decompression failed!");
assert!(&d == &[0]);
}

0 comments on commit f3299c8

Please sign in to comment.