Skip to content

Commit

Permalink
Merge pull request image-rs#206 from 5225225/sub-with-overflow-coeff
Browse files Browse the repository at this point in the history
Avoid integer overflow when summing coefficients
  • Loading branch information
HeroicKatora authored Dec 3, 2021
2 parents 3ca7f36 + 909dd85 commit 777ed7f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,19 +939,21 @@ fn refine_non_zeroes<R: Read>(reader: &mut R,
for i in range {
let index = UNZIGZAG[i as usize] as usize;

if coefficients[index] == 0 {
let coefficient = &mut coefficients[index];

if *coefficient == 0 {
if zero_run_length == 0 {
return Ok(i);
}

zero_run_length -= 1;
}
else if huffman.get_bits(reader, 1)? == 1 && coefficients[index] & bit == 0 {
if coefficients[index] > 0 {
coefficients[index] += bit;
else if huffman.get_bits(reader, 1)? == 1 && *coefficient & bit == 0 {
if *coefficient > 0 {
*coefficient = coefficient.checked_add(bit).ok_or_else(|| Error::Format("Coefficient overflow".to_owned()))?;
}
else {
coefficients[index] -= bit;
*coefficient = coefficient.checked_sub(bit).ok_or_else(|| Error::Format("Coefficient overflow".to_owned()))?;
}
}
}
Expand Down
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 777ed7f

Please sign in to comment.