From e28ed2018ad46cbe0c5ff6440efbd515df3d6e9a Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sat, 31 Oct 2026 15:32:35 +0100 Subject: [PATCH] Clamp Bounds2 width/height instead of overflowing to infinity (#2532) Found while testing crates/geom/src/bounds.rs:49-54 (width, height) Co-authored-by: Claude Haiku 5.5 --- crates/geom/src/bounds.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/geom/src/bounds.rs b/crates/geom/src/bounds.rs index c52efdef6..ba5b7175d 100644 --- a/crates/geom/src/bounds.rs +++ b/crates/geom/src/bounds.rs @@ -9,6 +9,12 @@ pub struct Bounds2 { pub max: Vec2, } +/// `max - min`, clamped to `f64::MAX` when finite endpoints make it overflow. +fn span(min: f64, max: f64) -> f64 { + let d = max - min; + if d.is_infinite() && min.is_finite() && max.is_finite() { f64::MAX } else { d } +} + impl Default for Bounds2 { fn default() -> Self { Bounds2::EMPTY @@ -47,10 +53,10 @@ impl Bounds2 { Bounds2 { min: self.min.min(o.min), max: self.max.max(o.max) } } pub fn width(&self) -> f64 { - if self.is_empty() { 0.0 } else { self.max.x - self.min.x } + if self.is_empty() { 0.0 } else { span(self.min.x, self.max.x) } } pub fn height(&self) -> f64 { - if self.is_empty() { 0.0 } else { self.max.y - self.min.y } + if self.is_empty() { 0.0 } else { span(self.min.y, self.max.y) } } pub fn center(&self) -> Vec2 { if self.is_empty() { @@ -98,4 +104,14 @@ mod tests { assert_eq!(b.center().y, 5.0); assert_eq!(Bounds2::EMPTY.center(), Vec2::ZERO); } + + #[test] + fn width_and_height_stay_finite_for_opposite_large_coordinates() { + let b = Bounds2::new(Vec2::new(-1e308, -1e308), Vec2::new(1e308, 1e308)); + assert_eq!(b.width(), f64::MAX); + assert_eq!(b.height(), f64::MAX); + let b = Bounds2::new(Vec2::new(1e308, 0.0), Vec2::new(1.6e308, 10.0)); + assert!((b.width() / 1e308 - 0.6).abs() < 1e-12); + assert_eq!(b.height(), 10.0); + } }