mirror of
https://github.com/tauri-apps/tao.git
synced 2026-01-31 00:35:16 +01:00
fix: return overflow in Icon::from_rgba (#958)
* fix: return overflow in `Icon::from_rgba` * Update icon.rs
This commit is contained in:
5
.changes/icon-dimensions-zero copy.md
Normal file
5
.changes/icon-dimensions-zero copy.md
Normal file
@@ -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,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
src/icon.rs
29
src/icon.rs
@@ -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),
|
||||
}
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user