Skip to content

Commit e70de2e

Browse files
fix(core): validate limits before fallible cloning
1 parent 35d58a3 commit e70de2e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎crates/pixl-core/src/lib.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl Image {
164164

165165
/// clones an image while enforcing limits and reporting allocation failure.
166166
pub fn try_clone(&self, limits: Limits) -> Result<Self, Error> {
167+
validate_dimensions(self.width, self.height, self.format, limits)?;
167168
let mut data = Vec::new();
168169
data.try_reserve_exact(self.data.len())
169170
.map_err(|_| Error::AllocationFailed)?;
@@ -471,6 +472,13 @@ mod tests {
471472
assert_eq!(img.pixel(2, 0), None);
472473
}
473474

475+
#[test]
476+
fn fallible_clone_enforces_limits() {
477+
let image = Image::new(2, 2, PixelFormat::Rgb8, Limits::default()).unwrap();
478+
let limits = Limits { max_dimension: 1, max_pixels: 1 };
479+
assert!(matches!(image.try_clone(limits), Err(Error::DimensionExceeded { .. })));
480+
}
481+
474482
#[test]
475483
fn fallible_conversion_enforces_limits() {
476484
let image = Image::new(2, 2, PixelFormat::Rgb8, Limits::default()).unwrap();
@@ -480,8 +488,16 @@ mod tests {
480488

481489
#[test]
482490
fn converts_la8_to_rgb_on_white() {
483-
let image = Image::from_data(1, 1, PixelFormat::La8, vec![100, 128], Limits::default()).unwrap();
484-
assert_eq!(image.try_to_rgb8_on_white(Limits::default()).unwrap().as_bytes(), &[177, 177, 177]);
491+
let image = Image::from_data(
492+
1,
493+
1,
494+
PixelFormat::La8,
495+
vec![100, 128],
496+
Limits::default(),
497+
)
498+
.unwrap();
499+
let rgb = image.try_to_rgb8_on_white(Limits::default()).unwrap();
500+
assert_eq!(rgb.as_bytes(), &[177, 177, 177]);
485501
}
486502

487503
#[test]

0 commit comments

Comments
 (0)