Skip to content

Commit de20379

Browse files
committedJan 5, 2025·
Avoid panic
1 parent b3fc1af commit de20379

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed
 

‎src/decoder.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,12 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
612612
}
613613

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

618622
if self.is_animated() {
619623
let saved = std::mem::take(&mut self.animation);

‎src/lossless.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,10 @@ impl<R: BufRead> BitReader<R> {
787787
let value = self.peek(num) as u32;
788788
self.consume(num)?;
789789

790-
match value.try_into() {
791-
Ok(value) => Ok(value),
792-
Err(_) => unreachable!("Value too large to fit in type"),
793-
}
790+
value.try_into().map_err(|_| {
791+
debug_assert!(false, "Value too large to fit in type");
792+
DecodingError::BitStreamError
793+
})
794794
}
795795
}
796796

0 commit comments

Comments
 (0)
Please sign in to comment.