@@ -92,6 +92,9 @@ pub struct Decoder<R> {
92
92
coefficients : Vec < Vec < i16 > > ,
93
93
// Bitmask of which coefficients has been completely decoded.
94
94
coefficients_finished : [ u64 ; MAX_COMPONENTS ] ,
95
+
96
+ // Maximum allowed size of decoded image buffer
97
+ decoding_buffer_size_limit : usize ,
95
98
}
96
99
97
100
impl < R : Read > Decoder < R > {
@@ -111,9 +114,15 @@ impl<R: Read> Decoder<R> {
111
114
exif_data : None ,
112
115
coefficients : Vec :: new ( ) ,
113
116
coefficients_finished : [ 0 ; MAX_COMPONENTS ] ,
117
+ decoding_buffer_size_limit : usize:: MAX ,
114
118
}
115
119
}
116
120
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
+
117
126
/// Returns metadata about the image.
118
127
///
119
128
/// 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> {
455
464
456
465
let frame = self . frame . as_ref ( ) . unwrap ( ) ;
457
466
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
+
458
471
// If we're decoding a progressive jpeg and a component is unfinished, render what we've got
459
472
if frame. coding_process == CodingProcess :: DctProgressive && self . coefficients . len ( ) == frame. components . len ( ) {
460
473
for ( i, component) in frame. components . iter ( ) . enumerate ( ) {
0 commit comments