Skip to content

Commit

Permalink
fix: return overflow in Icon::from_rgba (#958)
Browse files Browse the repository at this point in the history
* fix: return overflow in `Icon::from_rgba`

* Update icon.rs
  • Loading branch information
amrbashir authored Jul 25, 2024
1 parent fbc15c2 commit 1113935
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changes/icon-dimensions-zero copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Return a new `BadIcon::DimensionsZero` error variant in `Icon::from_rgba` if one of the passed icon dimensions is zero.
2 changes: 1 addition & 1 deletion .changes/icon-integer-overflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"tao": "patch"
---

Fix integer overflow when validating the icon data in `Icon::from_rgba`
Return a new `BadIcon::DimensionsMultiplyOverflow` error variant in `Icon::from_rgba` if dimensions multiplication overflowed.
29 changes: 23 additions & 6 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub enum BadIcon {
/// Produced when the provided icon width or height is equal to zero.
#[non_exhaustive]
DimensionsZero { width: u32, height: u32 },
/// Produced when the provided icon width or height is equal to zero.
#[non_exhaustive]
DimensionsMultiplyOverflow { width: u32, height: u32 },
/// Produced when underlying OS functionality failed to create the icon
OsError(io::Error),
}
Expand All @@ -59,10 +62,17 @@ impl fmt::Display for BadIcon {
BadIcon::DimensionsZero {
width,
height,
} => write!(f,
"The specified dimensions ({:?}x{:?}) must be greater than zero.",
width, height
),
} => write!(f,
"The specified dimensions ({:?}x{:?}) must be greater than zero.",
width, height
),
BadIcon::DimensionsMultiplyOverflow {
width,
height,
} => write!(f,
"The specified dimensions multiplication has overflowed ({:?}x{:?}).",
width, height
),
BadIcon::OsError(e) => write!(f, "OS error when instantiating the icon: {:?}", e),
}
}
Expand Down Expand Up @@ -104,12 +114,19 @@ mod constructors {
byte_count: rgba.len(),
});
}
let width_usize = width as usize;
let height_usize = height as usize;
let width_x_height = match width_usize.checked_mul(height_usize) {
Some(v) => v,
None => return Err(BadIcon::DimensionsMultiplyOverflow { width, height }),
};

let pixel_count = rgba.len() / PIXEL_SIZE;
if pixel_count != width as usize * height as usize {
if pixel_count != width_x_height {
Err(BadIcon::DimensionsVsPixelCount {
width,
height,
width_x_height: width as usize * height as usize,
width_x_height,
pixel_count,
})
} else {
Expand Down

0 comments on commit 1113935

Please sign in to comment.