fix(engine): Clone Stamp and Healing Brush refuse a far source instead of slicing out of bounds (#1111) (#1165)

`Mapping::offset()` saturates its `as i32` cast and `Rect::translate` saturates
each edge, so a far `source` or `offset` (3e9, i32::MAX) produced a sample
window narrower than the stroke bounds. `clone_sample` then relabelled that
region as the full-size rect and `Region::px` indexed past the buffer:
`range start index 120 out of range for slice of length 92` at
`paint/src/retouch.rs:54`, which the dispatch guard reported as an internal
error. #716 fixed the rotated/scaled branch (`source_rect`); this is the pure
translation branch.

`clone_sample` is now fallible: when the translated window does not keep the
stroke rect's size, the command returns a bad-params error naming the offset
and the document and history are untouched. A source far outside the canvas
but inside the i32 range still clones transparency, as before.

Test: `clone_and_heal_with_a_far_source_are_plain_errors` (both tools, the
issue's inputs, a negative far source, the untouched-layer and no-history-step
assertions, and the in-range far source that must keep working).

Co-authored-by: wstock <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-08 15:41:38 -04:00
1 parent 4860cef703
commit af3c99d817
2 files changed
+48 -6

No files matched your search

+20 -6
View File
@@ -195,15 +195,29 @@ fn clone_mapping(s: &mut Session, p: &Value, stroke: &Stroke, cmd: &str) -> Resu
/// Source pixels for destination `rect`: a translated read, or a bilinear resample when the
/// clone source is scaled, rotated or flipped.
fn clone_sample(pre: &Document, id: Option<LayerId>, surf: &Surface, which: SampleLayers, rect: Rect, map: &crate::presets::clone_source::Mapping) -> Region {
fn clone_sample(
pre: &Document,
id: Option<LayerId>,
surf: &Surface,
which: SampleLayers,
rect: Rect,
map: &crate::presets::clone_source::Mapping,
cmd: &str,
) -> Result<Region> {
if map.is_translation() {
let off = map.offset();
let mut r = sample(pre, id, surf, which, rect.translate(off.0, off.1));
let src = rect.translate(off.0, off.1);
// `translate` saturates at the i32 edges, so a far source (issue #1111) yields a window
// smaller than `rect`; relabelling that as `rect` would index past the sampled pixels.
if src.width() != rect.width() || src.height() != rect.height() {
return Err(bad(cmd, format!("source offset [{}, {}] is too far: the sample window leaves the coordinate range", off.0, off.1)));
}
let mut r = sample(pre, id, surf, which, src);
r.rect = rect;
return r;
return Ok(r);
}
let src = sample(pre, id, surf, which, map.source_rect(rect));
crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format()))
Ok(crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format())))
}
/// Result JSON shared by Clone Stamp and Healing Brush: the offset used and where the next stroke
@@ -251,7 +265,7 @@ fn clone_stamp(s: &mut Session, p: &Value) -> Result<Value> {
let dmg = run_stroke(s, "Clone Stamp", id, p, |pre, surf, sel, lock| {
let (bounds, cov) = stroke_coverage(&stroke);
// Samples come from the pre-stroke state, so pixels painted earlier in the stroke are never re-cloned.
let paint = clone_sample(pre, id, surf, which, bounds, &map);
let paint = clone_sample(pre, id, surf, which, bounds, &map, CMD)?;
Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode))
})?;
Ok(clone_result(dmg, off, aligned, p, &stroke))
@@ -307,7 +321,7 @@ fn healing_brush(s: &mut Session, p: &Value) -> Result<Value> {
let (bounds, cov) = stroke_coverage(&stroke);
let (g, cov) = pad_coverage(bounds, &cov, 2);
let fmt = surf.format();
let src = clone_sample(pre, id, surf, which, g, &map);
let src = clone_sample(pre, id, surf, which, g, &map, CMD)?;
// The destination the texture is fitted to: the layer itself, or what the user sees when
// sampling several layers (so healing onto an empty layer matches the composite).
let dst = if which == SampleLayers::Current { Region::read(surf, g) } else { composite_region(pre, id, which, g, fmt) };
+28
View File
@@ -874,3 +874,31 @@ fn content_aware_move_fails_gracefully() {
}
assert!(s.execute("paint.contentAwareMove", json!({"offset": [10, 0], "structure": 7.0, "color": null})).is_ok());
}
#[test]
fn clone_and_heal_with_a_far_source_are_plain_errors() {
// Issue #1111: a translation whose offset saturates `Rect::translate` used to sample a
// narrower window, relabel it full-size and index past the buffer (a panic the dispatch
// guard turned into an internal error). It is a bad-params error, and the layer is untouched.
for cmd in ["paint.cloneStamp", "paint.healingBrush"] {
let mut s = session(40, 30, 8, "rgb");
paint_layer(&mut s, texture);
let before = rgba(&s, 10, 10);
let steps = s.active().unwrap().history.past_len();
for p in [
json!({"points": [[10, 10]], "source": [3e9, 0]}),
json!({"points": [[10, 10]], "source": [0, -3e9]}),
json!({"points": [[10, 10]], "offset": [2147483647, 0]}),
json!({"points": [[10, 10], [30, 10]], "source": [2147483640, 0]}),
] {
let err = s.execute(cmd, p.clone()).expect_err(&format!("{cmd} {p}"));
assert!(matches!(err, EngineError::BadParams { .. }), "{cmd} {p}: {err:?}");
}
assert_eq!(rgba(&s, 10, 10), before, "{cmd}: a refused stroke changed the layer");
assert_eq!(s.active().unwrap().history.past_len(), steps, "{cmd}: a refused stroke recorded a history step");
// A source far outside the canvas but inside the coordinate range is still fine: it clones
// transparency.
s.execute(cmd, json!({"points": [[10, 10]], "source": [1e6, 1e6]})).unwrap();
s.execute(cmd, json!({"points": [[10, 10], [30, 10]], "source": [2147483000, 0]})).unwrap();
}
}