PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(geom): clamp huge sizes in Rect::from_xywh instead of wrapping, and saturate TileCoord::rect (#5161)
Saad Abdullahe1706507b5d9
diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rsindex 1a6b60fca..0e7673601 100644--- a/crates/geom/src/lib.rs+++ b/crates/geom/src/lib.rs@@ -46,7 +46,12 @@ impl Rect {Self { x0, y0, x1, y1 }}pub fn from_xywh(x: i32, y: i32, w: u32, h: u32) -> Self {- Self::new(x, y, x.saturating_add(w as i32), y.saturating_add(h as i32))+ // Widen before adding: `w as i32` wraps negative for widths above `i32::MAX`, which+ // would turn the far edge backwards (an empty, inverted rect) instead of clamping it+ // to `i32::MAX` the way `saturating_add` alone would suggest.+ let x1 = (i64::from(x) + i64::from(w)).min(i64::from(i32::MAX)) as i32;+ let y1 = (i64::from(y) + i64::from(h)).min(i64::from(i32::MAX)) as i32;+ Self::new(x, y, x1, y1)}pub fn from_size(size: Size) -> Self {Self::from_xywh(0, 0, size.width, size.height)@@ -117,7 +122,9 @@ impl TileCoord {Self::new(x.div_euclid(TILE_SIZE), y.div_euclid(TILE_SIZE))}pub fn rect(&self) -> Rect {- Rect::from_xywh(self.tx * TILE_SIZE, self.ty * TILE_SIZE, TILE_SIZE as u32, TILE_SIZE as u32)+ // `tx`/`ty` can come from a file (this type is deserialized), so saturate instead of+ // overflowing (a debug-build panic / release-build wrap) on extreme coordinates.+ Rect::from_xywh(self.tx.saturating_mul(TILE_SIZE), self.ty.saturating_mul(TILE_SIZE), TILE_SIZE as u32, TILE_SIZE as u32)}}@@ -258,6 +265,25 @@ mod tests {assert_eq!(grown.y0, i32::MIN);}+ #[test]+ fn from_xywh_clamps_huge_sizes_instead_of_wrapping() {+ // Widths above `i32::MAX` used to wrap negative through `w as i32`, producing an+ // empty inverted rect instead of clamping the far edge to `i32::MAX`.+ assert_eq!(Rect::from_xywh(0, 0, u32::MAX, 10), Rect::new(0, 0, i32::MAX, 10));+ assert_eq!(Rect::from_xywh(-100, 5, u32::MAX, u32::MAX), Rect::new(-100, 5, i32::MAX, i32::MAX));+ assert_eq!(Rect::from_xywh(10, 20, i32::MAX as u32 + 1, 40).width(), i32::MAX as u32 - 10);+ // Ordinary sizes are unchanged.+ assert_eq!(Rect::from_xywh(10, 20, 30, 40), Rect::new(10, 20, 40, 60));+ }++ #[test]+ fn tile_rect_saturates_on_extreme_coords() {+ // Tile coords are deserialized from files; `tx * TILE_SIZE` used to overflow.+ let r = TileCoord::new(i32::MAX, i32::MIN).rect();+ assert_eq!((r.x0, r.y0), (i32::MAX, i32::MIN));+ assert_eq!(TileCoord::new(2, -3).rect(), Rect::new(512, -768, 768, -512));+ }+#[test]fn width_and_height_do_not_overflow_on_extreme_edges() {// Inverted extreme rect: empty, so 0 (x1 - x0 used to overflow and panic).