Do checked multiplication for samples per strip

This commit is contained in:
5225225 committed 2021-05-29 12:37:15 +01:00
1 parent b41ac105ab
commit 2bb37a4ecd
2 files changed
+27 -2

No files matched your search

+7 -2
View File
@@ -1658,8 +1658,13 @@ impl<R: Read + Seek> Decoder<R> {
let rows_per_strip = let rows_per_strip =
usize::try_from(self.get_tag_u32(Tag::RowsPerStrip).unwrap_or(self.height))?; usize::try_from(self.get_tag_u32(Tag::RowsPerStrip).unwrap_or(self.height))?;
let samples_per_strip = let samples_per_strip = match usize::try_from(self.width)?
usize::try_from(self.width)? * rows_per_strip * self.bits_per_sample.len(); .checked_mul(rows_per_strip)
.and_then(|x| x.checked_mul(self.bits_per_sample.len()))
{
Some(s) => s,
None => return Err(TiffError::LimitsExceeded),
};
let mut result = let mut result =
self.result_buffer(usize::try_from(self.width)?, usize::try_from(self.height)?)?; self.result_buffer(usize::try_from(self.width)?, usize::try_from(self.height)?)?;
+20
View File
@@ -365,3 +365,23 @@ fn test_too_big_buffer_size() {
unexpected => panic!("Unexpected error {}", unexpected), unexpected => panic!("Unexpected error {}", unexpected),
} }
} }
#[test]
fn test_stripped_image_overflow() {
let image = [
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 100, 0, 0, 148, 1, 1, 4, 0, 1, 0,
0, 0, 158, 0, 0, 251, 3, 1, 3, 255, 254, 255, 255, 0, 1, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0,
0, 0, 0, 0, 17, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 2, 0, 0, 0, 63, 0, 0, 0, 22,
1, 4, 0, 1, 0, 0, 0, 44, 0, 248, 255, 23, 1, 4, 0, 1, 0, 0, 0, 178, 178, 178, 0, 1, 178,
178, 178,
];
let mut decoder = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap();
let err = decoder.read_image().unwrap_err();
match err {
tiff::TiffError::LimitsExceeded => {}
unexpected => panic!("Unexpected error {}", unexpected),
}
}