Use checked multiplication when calculating buffer size
This commit is contained in:
1 parent
1e10b9803b
commit
b41ac105ab
2 files changed
+27
-1
No files matched your search
+7
-1
@@ -1550,7 +1550,13 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn result_buffer(&self, width: usize, height: usize) -> TiffResult<DecodingResult> {
|
fn result_buffer(&self, width: usize, height: usize) -> TiffResult<DecodingResult> {
|
||||||
let buffer_size = width * height * self.bits_per_sample.len();
|
let buffer_size = match width
|
||||||
|
.checked_mul(height)
|
||||||
|
.and_then(|x| x.checked_mul(self.bits_per_sample.len()))
|
||||||
|
{
|
||||||
|
Some(s) => s,
|
||||||
|
None => return Err(TiffError::LimitsExceeded),
|
||||||
|
};
|
||||||
|
|
||||||
let max_sample_bits = self.bits_per_sample.iter().cloned().max().unwrap_or(8);
|
let max_sample_bits = self.bits_per_sample.iter().cloned().max().unwrap_or(8);
|
||||||
match self.sample_format.first().unwrap_or(&SampleFormat::Uint) {
|
match self.sample_format.first().unwrap_or(&SampleFormat::Uint) {
|
||||||
|
|||||||
@@ -345,3 +345,23 @@ fn test_bad_strip_count() {
|
|||||||
unexpected => panic!("Unexpected error {}", unexpected),
|
unexpected => panic!("Unexpected error {}", unexpected),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_too_big_buffer_size() {
|
||||||
|
let image = [
|
||||||
|
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 99, 255, 255, 254, 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, 9, 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, 0, 0, 23, 1, 4, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 1, 0, 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user