Skip to content

Commit 7e51cc9

Browse files
committed
Add limit for maximum output image
1 parent b15262f commit 7e51cc9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/decoder.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub struct Decoder<R> {
9292
coefficients: Vec<Vec<i16>>,
9393
// Bitmask of which coefficients has been completely decoded.
9494
coefficients_finished: [u64; MAX_COMPONENTS],
95+
96+
// Maximum allowed size of decoded image buffer
97+
decoding_buffer_size_limit: usize,
9598
}
9699

97100
impl<R: Read> Decoder<R> {
@@ -111,9 +114,15 @@ impl<R: Read> Decoder<R> {
111114
exif_data: None,
112115
coefficients: Vec::new(),
113116
coefficients_finished: [0; MAX_COMPONENTS],
117+
decoding_buffer_size_limit: usize::MAX,
114118
}
115119
}
116120

121+
/// Set maximum buffer size allowed for decoded images
122+
pub fn set_max_decoding_buffer_size(&mut self, max: usize) {
123+
self.decoding_buffer_size_limit = max;
124+
}
125+
117126
/// Returns metadata about the image.
118127
///
119128
/// The returned value will be `None` until a call to either `read_info` or `decode` has
@@ -455,6 +464,10 @@ impl<R: Read> Decoder<R> {
455464

456465
let frame = self.frame.as_ref().unwrap();
457466

467+
if frame.output_size.width as u64 * frame.output_size.height as u64 * frame.components.len() as u64 > self.decoding_buffer_size_limit as u64 {
468+
return Err(Error::Format("size of decoded image exceeds maximum allowed size".to_owned()));
469+
}
470+
458471
// If we're decoding a progressive jpeg and a component is unfinished, render what we've got
459472
if frame.coding_process == CodingProcess::DctProgressive && self.coefficients.len() == frame.components.len() {
460473
for (i, component) in frame.components.iter().enumerate() {

0 commit comments

Comments
 (0)