Skip to content

Commit

Permalink
Merge pull request image-rs#70 from kaksmet/overflow-fix
Browse files Browse the repository at this point in the history
Fix a possible integer overflow in derive_huffman_codes
  • Loading branch information
kaksmet committed Mar 18, 2017
2 parents 58f030c + e3d312e commit a2208c6
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,19 @@ fn derive_huffman_codes(bits: &[u8; 16]) -> Result<(Vec<u16>, Vec<u8>)> {
// Figure C.2
let mut huffcode = vec![0u16; huffsize.len()];
let mut code_size = huffsize[0];
let mut code = 0u16;
let mut code = 0u32;

for (i, &size) in huffsize.iter().enumerate() {
while code_size < size {
code <<= 1;
code_size += 1;
}

if code as u32 >= (1u32 << size) {
if code >= (1u32 << size) {
return Err(Error::Format("bad huffman code length".to_owned()));
}

huffcode[i] = code;
huffcode[i] = code as u16;
code += 1;
}

Expand Down
1 change: 1 addition & 0 deletions tests/crashtest/images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ File | Source
--------------------------| ------
imagetestsuite/ | The files in this directory were taken from https://code.google.com/p/imagetestsuite/
dc-predictor-overflow.jpg | Found by Wim Looman (@Nemo157) while fuzzing
derive-huffman-codes-overflow.jpg | Found by Pascal Hertleif (@killercup) while fuzzing
missing-sof.jpg | Found by Corey Farwell (@frewsxcv) when fuzz testing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a2208c6

Please sign in to comment.