From 111637db68d156ce6f54830667df00c1a5d9caae Mon Sep 17 00:00:00 2001 From: Maria Pospelova Date: Fri, 29 May 2026 09:53:57 +0000 Subject: [PATCH 1/4] Add lenient mode to ICO decoder --- src/codecs/ico/decoder.rs | 49 ++++++++++++++++++++++++++++++++++--- src/io/image_reader_type.rs | 5 +++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index b7fe270996..20ae7aa1b2 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -6,6 +6,7 @@ use crate::color::ColorType; use crate::error::{ DecodingError, ImageError, ImageResult, UnsupportedError, UnsupportedErrorKind, }; +use crate::io::image_reader_type::SpecCompliance; use crate::io::{ DecodedAnimationAttributes, DecodedImageAttributes, DecoderPreparedImage, FormatAttributes, }; @@ -113,6 +114,7 @@ pub struct IcoDecoder { selected_entry: DirEntry, reader_offset: u64, inner_decoder: InnerDecoder, + spec_strictness: SpecCompliance, } enum InnerDecoder { @@ -147,7 +149,15 @@ struct DirEntry { impl IcoDecoder { /// Create a new decoder that decodes from the stream ```r``` - pub fn new(mut r: R) -> ImageResult> { + pub fn new(r: R) -> ImageResult> { + Self::with_spec_compliance(r, SpecCompliance::default()) + } + + /// Create a new decoder with the given spec compliance mode. + pub(crate) fn with_spec_compliance( + mut r: R, + spec: SpecCompliance, + ) -> ImageResult> { let reader_offset = r.stream_position()?; let entries = read_entries(&mut r)?; let entry = best_entry(entries)?; @@ -157,6 +167,7 @@ impl IcoDecoder { selected_entry: entry, reader_offset, inner_decoder: decoder, + spec_strictness: spec, }) } } @@ -307,7 +318,9 @@ impl ImageDecoder for IcoDecoder { let layout = decoder.prepare_image()?; // Check if the image dimensions match the ones in the image data. let (width, height) = layout.layout.dimensions(); - if !self.selected_entry.matches_dimensions(width, height) { + if self.spec_strictness == SpecCompliance::Strict + && !self.selected_entry.matches_dimensions(width, height) + { return Err(DecoderError::ImageEntryDimensionMismatch { format: IcoEntryImageFormat::Png, entry: ( @@ -330,7 +343,9 @@ impl ImageDecoder for IcoDecoder { Bmp(decoder) => { let layout = decoder.prepare_image()?; let (width, height) = layout.layout.dimensions(); - if !self.selected_entry.matches_dimensions(width, height) { + if self.spec_strictness == SpecCompliance::Strict + && !self.selected_entry.matches_dimensions(width, height) + { return Err(DecoderError::ImageEntryDimensionMismatch { format: IcoEntryImageFormat::Bmp, entry: ( @@ -531,4 +546,32 @@ mod test { let mut buf = vec![0; usize::try_from(bytes).unwrap()]; assert!(decoder.read_image(&mut buf).is_err()); } + + #[test] + fn dimension_mismatch_strict_vs_lenient() { + // Minimal 2x2 32-bit BMP inside an ICO where the directory entry says 3x3. + let data = vec![ + 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x03, 0x03, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, + 0x40, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, + ]; + + let mut decoder = + IcoDecoder::with_spec_compliance(std::io::Cursor::new(&data), SpecCompliance::Lenient) + .unwrap(); + let bytes = decoder.prepare_image().unwrap().total_bytes(); + let mut buf = vec![0; usize::try_from(bytes).unwrap()]; + assert!(decoder.read_image(&mut buf).is_ok()); + + let mut decoder = + IcoDecoder::with_spec_compliance(std::io::Cursor::new(&data), SpecCompliance::Strict) + .unwrap(); + let bytes = decoder.prepare_image().unwrap().total_bytes(); + let mut buf = vec![0; usize::try_from(bytes).unwrap()]; + assert!(decoder.read_image(&mut buf).is_err()); + } } diff --git a/src/io/image_reader_type.rs b/src/io/image_reader_type.rs index 6b33a6be75..663894be81 100644 --- a/src/io/image_reader_type.rs +++ b/src/io/image_reader_type.rs @@ -220,7 +220,10 @@ impl<'a, R: 'a + BufRead + Seek> ImageReaderOptions { spec_compliance, )?), #[cfg(feature = "ico")] - ImageFormat::Ico => Box::new(ico::IcoDecoder::new(reader)?), + ImageFormat::Ico => Box::new(ico::IcoDecoder::with_spec_compliance( + reader, + spec_compliance, + )?), #[cfg(feature = "hdr")] ImageFormat::Hdr => Box::new(hdr::HdrDecoder::with_spec_compliance( reader, From bfb0e920f0118563dfe6f01abeffb4b1aaa49ce0 Mon Sep 17 00:00:00 2001 From: Maria Pospelova Date: Tue, 2 Jun 2026 07:46:36 +0000 Subject: [PATCH 2/4] Add PNG lenient mode to ICO decoder --- src/codecs/ico/decoder.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index 20ae7aa1b2..a9aa5871cb 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -161,7 +161,7 @@ impl IcoDecoder { let reader_offset = r.stream_position()?; let entries = read_entries(&mut r)?; let entry = best_entry(entries)?; - let decoder = entry.decoder(r, reader_offset)?; + let decoder = entry.decoder(r, reader_offset, spec)?; Ok(IcoDecoder { selected_entry: entry, @@ -267,17 +267,22 @@ impl DirEntry { &self, mut r: R, reader_offset: u64, + spec: SpecCompliance, ) -> ImageResult> { let is_png = self.is_png(&mut r, reader_offset)?; self.seek_to_start(&mut r, reader_offset)?; if is_png { - let limits = crate::Limits { - max_image_width: Some(self.real_width().into()), - max_image_height: Some(self.real_height().into()), - max_alloc: Some(256 * 256 * 4 * 2), // width * height * 4 bytes per pixel * safety factor of 2 - }; - Ok(Png(Box::new(PngDecoder::with_limits(r, limits)))) + if spec == SpecCompliance::Strict { + let limits = crate::Limits { + max_image_width: Some(self.real_width().into()), + max_image_height: Some(self.real_height().into()), + max_alloc: Some(256 * 256 * 4 * 2), // width * height * 4 bytes per pixel * safety factor of 2 + }; + Ok(Png(Box::new(PngDecoder::with_limits(r, limits)))) + } else { + Ok(Png(Box::new(PngDecoder::new(r)))) + } } else { Ok(Bmp(BmpDecoder::new_with_ico_format(r)?)) } From 012113b6ae57654a3585a49637e64fc3b9904736 Mon Sep 17 00:00:00 2001 From: mpospelova <40293309+mpospelova@users.noreply.github.com> Date: Wed, 3 Jun 2026 14:32:10 +0200 Subject: [PATCH 3/4] Update src/codecs/ico/decoder.rs Co-authored-by: Michael Schmidt --- src/codecs/ico/decoder.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index a9aa5871cb..f148361759 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -273,16 +273,7 @@ impl DirEntry { self.seek_to_start(&mut r, reader_offset)?; if is_png { - if spec == SpecCompliance::Strict { - let limits = crate::Limits { - max_image_width: Some(self.real_width().into()), - max_image_height: Some(self.real_height().into()), - max_alloc: Some(256 * 256 * 4 * 2), // width * height * 4 bytes per pixel * safety factor of 2 - }; - Ok(Png(Box::new(PngDecoder::with_limits(r, limits)))) - } else { - Ok(Png(Box::new(PngDecoder::new(r)))) - } + Ok(Png(Box::new(PngDecoder::new(r)))) } else { Ok(Bmp(BmpDecoder::new_with_ico_format(r)?)) } From 3b6938395b4ec5a58ec4ab5cd03d4747243245b1 Mon Sep 17 00:00:00 2001 From: Maria Pospelova Date: Wed, 3 Jun 2026 13:06:49 +0000 Subject: [PATCH 4/4] Remove unused variable --- src/codecs/ico/decoder.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index f148361759..9d7fca5c0d 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -161,7 +161,7 @@ impl IcoDecoder { let reader_offset = r.stream_position()?; let entries = read_entries(&mut r)?; let entry = best_entry(entries)?; - let decoder = entry.decoder(r, reader_offset, spec)?; + let decoder = entry.decoder(r, reader_offset)?; Ok(IcoDecoder { selected_entry: entry, @@ -267,7 +267,6 @@ impl DirEntry { &self, mut r: R, reader_offset: u64, - spec: SpecCompliance, ) -> ImageResult> { let is_png = self.is_png(&mut r, reader_offset)?; self.seek_to_start(&mut r, reader_offset)?;