From 18d1b8c7f7757dee1c1342336e3e83ef5dedbd47 Mon Sep 17 00:00:00 2001 From: Ivan Sraichuk Date: Mon, 18 Mar 2024 19:11:45 +0100 Subject: [PATCH 1/3] Improve error messages --- src/codecs/ico/decoder.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index 0ad4cf613d..291843f080 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -18,12 +18,12 @@ enum DecoderError { /// The ICO directory is empty NoEntries, /// The number of color planes (0 or 1), or the horizontal coordinate of the hotspot for CUR files too big. - IcoEntryTooManyPlanesOrHotspot, + IcoEntryTooManyPlanesOrHotspot(u16), /// The bit depth (may be 0 meaning unspecified), or the vertical coordinate of the hotspot for CUR files too big. - IcoEntryTooManyBitsPerPixelOrHotspot, + IcoEntryTooManyBitsPerPixelOrHotspot(u16), /// The entry is in PNG format and specified a length that is shorter than PNG header. - PngShorterThanHeader, + PngShorterThanHeader(u32), /// The enclosed PNG is not in RGBA, which is invalid: https://blogs.msdn.microsoft.com/oldnewthing/20101022-00/?p=12473/. PngNotRgba, @@ -45,14 +45,14 @@ impl fmt::Display for DecoderError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { DecoderError::NoEntries => f.write_str("ICO directory contains no image"), - DecoderError::IcoEntryTooManyPlanesOrHotspot => { - f.write_str("ICO image entry has too many color planes or too large hotspot value") - } - DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot => f.write_str( - "ICO image entry has too many bits per pixel or too large hotspot value", - ), - DecoderError::PngShorterThanHeader => { - f.write_str("Entry specified a length that is shorter than PNG header!") + DecoderError::IcoEntryTooManyPlanesOrHotspot(num_color_planes) => { + f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of color planes or has an invalid hotspot value", num_color_planes)) + }, + DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot(bits_per_pixel) => { + f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of bits per pixel or has an invalid hotspot value", bits_per_pixel)) + }, + DecoderError::PngShorterThanHeader(length) => { + f.write_fmt(format_args!("Entry specifies a length of {} which is shorter than the PNG header!", length)) } DecoderError::PngNotRgba => f.write_str("The PNG is not in RGBA format!"), DecoderError::InvalidDataSize => { @@ -173,7 +173,7 @@ fn read_entry(r: &mut R) -> ImageResult { // of the hotspot for CUR files. let num = r.read_u16::()?; if num > 256 { - return Err(DecoderError::IcoEntryTooManyPlanesOrHotspot.into()); + return Err(DecoderError::IcoEntryTooManyPlanesOrHotspot(num).into()); } num }, @@ -182,7 +182,7 @@ fn read_entry(r: &mut R) -> ImageResult { // or the vertical coordinate of the hotspot for CUR files. let num = r.read_u16::()?; if num > 256 { - return Err(DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot.into()); + return Err(DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot(num).into()); } num }, @@ -280,7 +280,10 @@ impl ImageDecoder for IcoDecoder { match self.inner_decoder { Png(decoder) => { if self.selected_entry.image_length < PNG_SIGNATURE.len() as u32 { - return Err(DecoderError::PngShorterThanHeader.into()); + return Err(DecoderError::PngShorterThanHeader( + self.selected_entry.image_length, + ) + .into()); } // Check if the image dimensions match the ones in the image data. From ef06ebd1c190787b5b24f3f04551afe8a46203f5 Mon Sep 17 00:00:00 2001 From: Ivan Sraichuk Date: Mon, 1 Apr 2024 22:09:48 +0200 Subject: [PATCH 2/3] Trying to improve error message --- src/codecs/ico/decoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index 291843f080..f0e07dd1e0 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -46,7 +46,7 @@ impl fmt::Display for DecoderError { match self { DecoderError::NoEntries => f.write_str("ICO directory contains no image"), DecoderError::IcoEntryTooManyPlanesOrHotspot(num_color_planes) => { - f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of color planes or has an invalid hotspot value", num_color_planes)) + f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of color planes for ICO or has an invalid hotspot for CUR files", num_color_planes)) }, DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot(bits_per_pixel) => { f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of bits per pixel or has an invalid hotspot value", bits_per_pixel)) From f80256ccc13b13d58102ef402792a2076031e315 Mon Sep 17 00:00:00 2001 From: Ivan Sraichuk Date: Mon, 1 Apr 2024 22:27:28 +0200 Subject: [PATCH 3/3] Trying to improve error message --- src/codecs/ico/decoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codecs/ico/decoder.rs b/src/codecs/ico/decoder.rs index f0e07dd1e0..475d96c92e 100644 --- a/src/codecs/ico/decoder.rs +++ b/src/codecs/ico/decoder.rs @@ -49,7 +49,7 @@ impl fmt::Display for DecoderError { f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of color planes for ICO or has an invalid hotspot for CUR files", num_color_planes)) }, DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot(bits_per_pixel) => { - f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of bits per pixel or has an invalid hotspot value", bits_per_pixel)) + f.write_fmt(format_args!("ICO image entry specifies {}, which exceeds the allowed number of bits per pixel for ICO or has an invalid hotspot value for CUR files.", bits_per_pixel)) }, DecoderError::PngShorterThanHeader(length) => { f.write_fmt(format_args!("Entry specifies a length of {} which is shorter than the PNG header!", length))