From 84498b832239d879fc9f16628cf84c54ea1d678c Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Wed, 7 Jun 2023 23:11:47 +0800 Subject: [PATCH] Fix: Small dict leads to panic Fixes #131 that lead to a panic when the dict length was smaller than 4. A match has the minimum length of 4 bytes, smaller dicts will be ignored --- src/block/compress.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/block/compress.rs b/src/block/compress.rs index 8f886726..708d3993 100644 --- a/src/block/compress.rs +++ b/src/block/compress.rs @@ -662,10 +662,13 @@ pub fn compress_into_with_dict( fn compress_into_vec_with_dict( input: &[u8], prepend_size: bool, - dict_data: &[u8], + mut dict_data: &[u8], ) -> Vec { let prepend_size_num_bytes = if prepend_size { 4 } else { 0 }; let max_compressed_size = get_maximum_output_size(input.len()) + prepend_size_num_bytes; + if dict_data.len() <= 3 { + dict_data = b""; + } #[cfg(feature = "safe-encode")] let mut compressed = { let mut compressed: Vec = vec![0u8; max_compressed_size]; @@ -877,6 +880,15 @@ mod tests { assert_eq!(input, uncompressed); } + #[test] + fn test_dict_no_panic() { + let input: &[u8] = &[ + 10, 12, 14, 16, 18, 10, 12, 14, 16, 18, 10, 12, 14, 16, 18, 10, 12, 14, 16, 18, + ]; + let dict = &[10, 12, 14]; + let _compressed = compress_with_dict(input, dict); + } + #[test] fn test_dict_match_crossing() { let input: &[u8] = &[