mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
75e967b467
commit
ff0f1ba0c5
3 files changed
+54
-8
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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<Vec<f64>>) -> 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)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user