Skip to content

Commit

Permalink
Avoid panic
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 4, 2025
1 parent f798911 commit ef7ac54
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,12 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
}

/// Returns the raw bytes of the image. For animated images, this is the first frame.
///
/// Fails with `ImageTooLarge` if `buf` has length different than `output_buffer_size()`
pub fn read_image(&mut self, buf: &mut [u8]) -> Result<(), DecodingError> {
assert_eq!(Some(buf.len()), self.output_buffer_size());
if Some(buf.len()) != self.output_buffer_size() {
return Err(DecodingError::ImageTooLarge);
}

if self.is_animated() {
let saved = std::mem::take(&mut self.animation);
Expand Down
8 changes: 4 additions & 4 deletions src/lossless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,10 @@ impl<R: BufRead> BitReader<R> {
let value = self.peek(num) as u32;
self.consume(num)?;

match value.try_into() {
Ok(value) => Ok(value),
Err(_) => unreachable!("Value too large to fit in type"),
}
value.try_into().map_err(|_| {
debug_assert!(false, "Value too large to fit in type");
DecodingError::BitStreamError
})
}
}

Expand Down

0 comments on commit ef7ac54

Please sign in to comment.