Skip to content

Commit

Permalink
Merge pull request #238 from johannesvollmer/fix-237-subtraction-over…
Browse files Browse the repository at this point in the history
…flow-panic

fix unchecked subtraction overflow resulting in panic
  • Loading branch information
johannesvollmer authored Oct 3, 2024
2 parents e018e0d + 9732d1d commit ce6ec41
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/compression/piz/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ fn decode_with_tables(

let count = u64::try_from((8 - input_bit_count) & 7)?;
code_bits >>= count;
code_bit_count -= count;

code_bit_count = code_bit_count.checked_sub(count)
.ok_or_else(|| Error::invalid("code"))?;

while code_bit_count > 0 {
let index = (code_bits << (DECODE_BITS - code_bit_count)) & DECODE_MASK;
Expand Down Expand Up @@ -294,7 +296,7 @@ fn read_encoding_table(
let mut code_bit_count = 0_u64;

// TODO push() into encoding table instead of index stuff?
let mut encoding_table = vec![0_u64; ENCODING_TABLE_SIZE];
let mut encoding_table = vec![0_u64; ENCODING_TABLE_SIZE];
let mut code_index = min_code_index;
while code_index <= max_code_index {
let code_len = read_bits(6, &mut code_bits, &mut code_bit_count, packed)?;
Expand Down

0 comments on commit ce6ec41

Please sign in to comment.