Skip to content

Commit 2d83a3d

Browse files
committed
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
1 parent 15e4ffa commit 2d83a3d

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/block/compress.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,13 @@ pub fn compress_into_with_dict(
662662
fn compress_into_vec_with_dict<const USE_DICT: bool>(
663663
input: &[u8],
664664
prepend_size: bool,
665-
dict_data: &[u8],
665+
mut dict_data: &[u8],
666666
) -> Vec<u8> {
667667
let prepend_size_num_bytes = if prepend_size { 4 } else { 0 };
668668
let max_compressed_size = get_maximum_output_size(input.len()) + prepend_size_num_bytes;
669+
if dict_data.len() <= 3 {
670+
dict_data = b"";
671+
}
669672
#[cfg(feature = "safe-encode")]
670673
let mut compressed = {
671674
let mut compressed: Vec<u8> = vec![0u8; max_compressed_size];
@@ -877,6 +880,15 @@ mod tests {
877880
assert_eq!(input, uncompressed);
878881
}
879882

883+
#[test]
884+
fn test_dict_no_panic() {
885+
let input: &[u8] = &[
886+
10, 12, 14, 16, 18, 10, 12, 14, 16, 18, 10, 12, 14, 16, 18, 10, 12, 14, 16, 18,
887+
];
888+
let dict = &[10, 12, 14];
889+
let _compressed = compress_with_dict(input, dict);
890+
}
891+
880892
#[test]
881893
fn test_dict_match_crossing() {
882894
let input: &[u8] = &[

0 commit comments

Comments
 (0)