Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvement: Adjust error messages. #2172

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/codecs/ico/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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 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 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))
}
DecoderError::PngNotRgba => f.write_str("The PNG is not in RGBA format!"),
DecoderError::InvalidDataSize => {
Expand Down Expand Up @@ -173,7 +173,7 @@ fn read_entry<R: Read>(r: &mut R) -> ImageResult<DirEntry> {
// of the hotspot for CUR files.
let num = r.read_u16::<LittleEndian>()?;
if num > 256 {
return Err(DecoderError::IcoEntryTooManyPlanesOrHotspot.into());
return Err(DecoderError::IcoEntryTooManyPlanesOrHotspot(num).into());
}
num
},
Expand All @@ -182,7 +182,7 @@ fn read_entry<R: Read>(r: &mut R) -> ImageResult<DirEntry> {
// or the vertical coordinate of the hotspot for CUR files.
let num = r.read_u16::<LittleEndian>()?;
if num > 256 {
return Err(DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot.into());
return Err(DecoderError::IcoEntryTooManyBitsPerPixelOrHotspot(num).into());
}
num
},
Expand Down Expand Up @@ -280,7 +280,10 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
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.
Expand Down
Loading