From ff0f1ba0c55136b73e96f2fef17c1eece80da79c Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Tue, 3 Nov 2026 09:27:55 +0000 Subject: [PATCH 1/4] fix(algo): Liquify replays a stroke with an absurd brush size or position without overflowing A stroke in a project file may name any f64 for its size and points. The dab turned them into grid indexes and a snapshot margin with bare casts: a position past the grid saturated to usize::MAX, wrapped negative as an isize, slipped past the emptiness check and underflowed the snapshot bounds; the margin and the dirty-rect padding overflowed too. The indexes are now clamped to the grid before the casts and the margins saturate. The new test fails without the change (attempt to subtract with overflow at liquify.rs:496). Co-Authored-By: Claude Sonnet 5.5 --- crates/algo/src/liquify.rs | 24 ++++++++---- .../algo/tests/it/liquify_extreme_stroke.rs | 37 +++++++++++++++++++ crates/algo/tests/it/main.rs | 1 + 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 crates/algo/tests/it/liquify_extreme_stroke.rs 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; From 200f38f7b60600096627762f1d6c5f320ad1274a Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Mon, 9 Nov 2026 20:19:40 +0000 Subject: [PATCH 2/4] Liquify: a huge far-off dab writes no NaN and reports the area it changed Review on #11013. A dab whose centre and size are both huge gave infinity over infinity for a node's distance, and the NaN slipped past the falloff check. The changed rectangle is now worked out in f64 before the cast, so a far-off centre with a radius reaching the image no longer reports empty. Co-Authored-By: Claude --- crates/algo/src/liquify.rs | 16 ++++++------- .../algo/tests/it/liquify_extreme_stroke.rs | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index a3238eac3d..5b3e39ae0e 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -680,7 +680,9 @@ impl LiquifyField { let p = self.node_pos(i, j); let (dx, dy) = (p[0] - c[0], p[1] - c[1]); let t2 = (dx * dx + dy * dy) / (r * r); - if t2 >= 1.0 { + // NaN is skipped too: it is what a far-off centre with a huge radius gives + // (infinity over infinity): a NaN weight would spread NaN displacements. + if t2.is_nan() || t2 >= 1.0 { continue; } let fall = falloff(t2, density); @@ -749,14 +751,10 @@ impl LiquifyField { self.d[idx] = d; self.freeze[idx] = f; } - 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) + // Each edge is worked out in f64 and only then converted (saturating), so a far-off centre + // with a huge radius still covers the nodes it changed. + let pad = r.ceil() + 2.0; + Rect::new((c[0].floor() - pad) as i32, (c[1].floor() - pad) as i32, (c[0].ceil() + pad) as i32, (c[1].ceil() + pad) as i32).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 index fca07ddb39..492f98599b 100644 --- a/crates/algo/tests/it/liquify_extreme_stroke.rs +++ b/crates/algo/tests/it/liquify_extreme_stroke.rs @@ -35,3 +35,26 @@ fn a_brush_with_an_absurd_size_or_position_replays_without_overflow() { } } } + +/// Review on #11013: a dab whose centre and size are both huge gives infinity over infinity for its +/// distance; it must not write NaN displacements. +#[test] +fn a_huge_far_off_dab_leaves_the_field_finite() { + let bounds = Rect::new(0, 0, 64, 64); + for tool in BRUSH_TOOLS { + let f = LiquifyField::from_strokes(bounds, 8.0, &[stroke(tool, 4e200, vec![vec![1e200, 10.0]])]); + assert!(f.max_displacement().is_finite(), "{tool:?}"); + assert!(f.sample(10.0, 10.0).iter().all(|v| v.is_finite()), "{tool:?}"); + } +} + +/// Review on #11013: a Freeze dab centred far to the left with a radius reaching the image changes +/// nodes inside it, so the area it reports must not be empty. +#[test] +fn a_far_off_dab_that_reaches_the_image_reports_what_it_changed() { + let mut f = LiquifyField::new(Rect::new(0, 0, 64, 64), 8.0); + let changed = f.stroke_begin(&stroke(LiquifyTool::Freeze, 1.2e10, vec![]), [-3e9, 10.0, 1.0]); + if f.freeze_at(10.0, 10.0) > 0.0 { + assert!(!changed.is_empty(), "the freeze changed nodes but reported {changed:?}"); + } +} From 7d5f68ddc60c0e9b8f402ed78d779eeebfea1b24 Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Mon, 9 Nov 2026 20:30:58 +0000 Subject: [PATCH 3/4] Liquify test: require the far-off Freeze dab to change the field Review on #11013: the check on the reported area no longer depends on a condition. Co-Authored-By: Claude --- crates/algo/tests/it/liquify_extreme_stroke.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/algo/tests/it/liquify_extreme_stroke.rs b/crates/algo/tests/it/liquify_extreme_stroke.rs index 492f98599b..79dc896c74 100644 --- a/crates/algo/tests/it/liquify_extreme_stroke.rs +++ b/crates/algo/tests/it/liquify_extreme_stroke.rs @@ -54,7 +54,6 @@ fn a_huge_far_off_dab_leaves_the_field_finite() { fn a_far_off_dab_that_reaches_the_image_reports_what_it_changed() { let mut f = LiquifyField::new(Rect::new(0, 0, 64, 64), 8.0); let changed = f.stroke_begin(&stroke(LiquifyTool::Freeze, 1.2e10, vec![]), [-3e9, 10.0, 1.0]); - if f.freeze_at(10.0, 10.0) > 0.0 { - assert!(!changed.is_empty(), "the freeze changed nodes but reported {changed:?}"); - } + assert!(f.freeze_at(10.0, 10.0) > 0.0, "the dab reaches the image, so it freezes nodes there"); + assert!(!changed.is_empty(), "the freeze changed nodes but reported {changed:?}"); } From 165cab50ad634bccc568e07dc096202cfd8aeafc Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Mon, 9 Nov 2026 20:38:58 +0000 Subject: [PATCH 4/4] Liquify test: document the stroke helper and the replay test Co-Authored-By: Claude --- crates/algo/tests/it/liquify_extreme_stroke.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/algo/tests/it/liquify_extreme_stroke.rs b/crates/algo/tests/it/liquify_extreme_stroke.rs index 79dc896c74..d7686c9681 100644 --- a/crates/algo/tests/it/liquify_extreme_stroke.rs +++ b/crates/algo/tests/it/liquify_extreme_stroke.rs @@ -19,10 +19,12 @@ const BRUSH_TOOLS: [LiquifyTool; 10] = [ const EXTREME: [f64; 9] = [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::MAX, f64::MIN, 1e300, 3.0e9, -1.0, 0.0]; +/// A stroke of `tool` with brush `size` through `points`, at middling density, pressure and rate. fn stroke(tool: LiquifyTool, size: f64, points: Vec>) -> LiquifyStroke { LiquifyStroke { tool, size, density: 50.0, pressure: 50.0, rate: 50.0, points, amount: None } } +/// Every brush tool, with every absurd size and position, replays without a panic. #[test] fn a_brush_with_an_absurd_size_or_position_replays_without_overflow() { let bounds = Rect::new(0, 0, 64, 64);