diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index 40865c09b2..aec3697aff 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -481,18 +481,20 @@ impl LiquifyField { } let (gx0, gy0) = self.to_grid(c[0] - r, c[1] - r); let (gx1, gy1) = self.to_grid(c[0] + r, c[1] + r); - let i0 = gx0.floor().max(0.0) as usize; - let j0 = gy0.floor().max(0.0) as usize; - let i1 = (gx1.ceil().max(-1.0) as isize).min(self.w as isize - 1); - let j1 = (gy1.ceil().max(-1.0) as isize).min(self.h as isize - 1); + // Held to the grid before the casts: a brush at an absurd position saturates a bare cast to + // usize::MAX, which wraps negative as an isize and slips past the emptiness check below. + let i0 = gx0.floor().clamp(0.0, self.w as f64) as usize; + let j0 = gy0.floor().clamp(0.0, self.h as f64) as usize; + let i1 = (gx1.ceil().clamp(-1.0, self.w as f64) as isize).min(self.w as isize - 1); + let j1 = (gy1.ceil().clamp(-1.0, self.h as f64) as isize).min(self.h as isize - 1); if i1 < i0 as isize || j1 < j0 as isize { return Rect::EMPTY; } let (i1, j1) = (i1 as usize, j1 as usize); // Snapshot of the affected nodes plus a margin (sampling reads around them). - let m = ((delta[0].abs().max(delta[1].abs()) + r * 0.2) / self.cell).ceil() as usize + 2; + let m = (((delta[0].abs().max(delta[1].abs()) + r * 0.2) / self.cell).ceil() as usize).saturating_add(2); let (si0, sj0) = (i0.saturating_sub(m), j0.saturating_sub(m)); - let (si1, sj1) = ((i1 + m).min(self.w - 1), (j1 + m).min(self.h - 1)); + let (si1, sj1) = (i1.saturating_add(m).min(self.w - 1), j1.saturating_add(m).min(self.h - 1)); let sw = si1 - si0 + 1; let sh = sj1 - sj0 + 1; let mut snap = Vec::with_capacity(sw * sh); @@ -586,8 +588,14 @@ impl LiquifyField { self.d[idx] = d; self.freeze[idx] = f; } - let pad = r.ceil() as i32 + 2; - Rect::new(c[0].floor() as i32 - pad, c[1].floor() as i32 - pad, c[0].ceil() as i32 + pad, c[1].ceil() as i32 + pad).intersect(&self.bounds) + let pad = (r.ceil() as i32).saturating_add(2); + Rect::new( + (c[0].floor() as i32).saturating_sub(pad), + (c[1].floor() as i32).saturating_sub(pad), + (c[0].ceil() as i32).saturating_add(pad), + (c[1].ceil() as i32).saturating_add(pad), + ) + .intersect(&self.bounds) } /// Replays strokes on a fresh field. diff --git a/crates/algo/tests/it/liquify_extreme_stroke.rs b/crates/algo/tests/it/liquify_extreme_stroke.rs new file mode 100644 index 0000000000..fca07ddb39 --- /dev/null +++ b/crates/algo/tests/it/liquify_extreme_stroke.rs @@ -0,0 +1,37 @@ +//! A project file stores Liquify strokes as plain numbers, so a stroke may name any `f64` for its +//! brush size or its points. Replaying one must never overflow, however absurd the values. + +use photocraft_algo::liquify::{LiquifyField, LiquifyStroke, LiquifyTool}; +use photocraft_geom::Rect; + +const BRUSH_TOOLS: [LiquifyTool; 10] = [ + LiquifyTool::ForwardWarp, + LiquifyTool::Reconstruct, + LiquifyTool::Smooth, + LiquifyTool::TwirlCw, + LiquifyTool::TwirlCcw, + LiquifyTool::Pucker, + LiquifyTool::Bloat, + LiquifyTool::PushLeft, + LiquifyTool::Freeze, + LiquifyTool::Thaw, +]; + +const EXTREME: [f64; 9] = [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::MAX, f64::MIN, 1e300, 3.0e9, -1.0, 0.0]; + +fn stroke(tool: LiquifyTool, size: f64, points: Vec>) -> LiquifyStroke { + LiquifyStroke { tool, size, density: 50.0, pressure: 50.0, rate: 50.0, points, amount: None } +} + +#[test] +fn a_brush_with_an_absurd_size_or_position_replays_without_overflow() { + let bounds = Rect::new(0, 0, 64, 64); + for tool in BRUSH_TOOLS { + for size in EXTREME { + for p in [0.0, 1e300, f64::NAN, 3.0e9, -3.0e9] { + let points = vec![vec![p, 10.0], vec![20.0, p], vec![p, p, f64::MAX]]; + let _ = LiquifyField::from_strokes(bounds, 8.0, &[stroke(tool, size, points)]); + } + } + } +} diff --git a/crates/algo/tests/it/main.rs b/crates/algo/tests/it/main.rs index 1f26e46b8e..3a68f94728 100644 --- a/crates/algo/tests/it/main.rs +++ b/crates/algo/tests/it/main.rs @@ -1,5 +1,6 @@ //! The crate's integration tests, linked into one test binary. +mod liquify_extreme_stroke; mod liquify_freeze_formats; mod liquify_source_footprint; mod retouch_blur_exact;