From 425b5efeedcad19b868d00ee7f476e4077aed8b2 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 27 Oct 2026 18:18:48 +0100 Subject: [PATCH] Integration: a painted fill reserves its buffers before it builds them (#3779) CodeRabbit on #9775: paint.stroke with fill:true builds the coverage, its feathered copy, the selection read and the mask over the region, up to the whole canvas (file.new allows 300000 x 300000), with infallible allocations that abort the process when memory runs out. The peak is now reserved through the fallible allocator first, and the selection is read through it; fill_survives_allocation_failure fails without this (the error came from a later blend). Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/engine/src/cmd/brush_cmds.rs | 9 +++++++-- crates/engine/src/cmd/brush_cmds/tests.rs | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/brush_cmds.rs b/crates/engine/src/cmd/brush_cmds.rs index 05b2911831..05af865ddc 100644 --- a/crates/engine/src/cmd/brush_cmds.rs +++ b/crates/engine/src/cmd/brush_cmds.rs @@ -484,14 +484,19 @@ fn fill_region(s: &mut Session, p: &Value, label: &str, brush: BrushSettings, pt return Ok(Rect::new(0, 0, 0, 0)); } let (w, h) = (bounds.width() as usize, bounds.height() as usize); + // The coverage, its feathered copy (and the blur's scratch), the selection read and + // the mask are each a buffer over the whole region, which can be the whole canvas: + // reserve that peak first, so a region too big for memory is an error, not an abort. + const FILL: &str = "filling a painted region"; + let peak = w.checked_mul(h).and_then(|n| n.checked_mul(5)).ok_or_else(|| crate::allocation::error(FILL, "requested size overflowed"))?; + drop(crate::allocation::capacity::(peak, FILL)?); let xy: Vec<(f32, f32)> = pts.iter().map(|sp| (sp.x as f32, sp.y as f32)).collect(); let mut cov = photocraft_algo::selection::polygon(&xy, bounds, true); if feather_px > 0.01 { cov = photocraft_algo::selection::feather(&cov, w, h, feather_px); } if let Some(sel) = sel.as_ref() { - let mut sv = Vec::new(); - sel.read_region_into(bounds, &mut sv); + let sv = crate::allocation::read_region(sel, bounds, FILL)?; for (c, s) in cov.iter_mut().zip(&sv) { *c *= s.clamp(0.0, 1.0); } diff --git a/crates/engine/src/cmd/brush_cmds/tests.rs b/crates/engine/src/cmd/brush_cmds/tests.rs index 32b8efa708..3a3041c4a6 100644 --- a/crates/engine/src/cmd/brush_cmds/tests.rs +++ b/crates/engine/src/cmd/brush_cmds/tests.rs @@ -580,6 +580,23 @@ fn define_brush_from_selection() { assert!(rgba(&s, 30, 32)[3] > 0.9); } +#[test] +fn fill_survives_allocation_failure() { + // CodeRabbit on #9775: a fill's buffers span the region, up to the whole canvas. Their peak is + // reserved through the fallible allocator before any of them is built, so running out of + // memory is the fill's own error, leaving the pixels and history alone, never an abort. + let mut s = session(60, 60); + let pixels_before = surface(&s); + let history_before = s.active().unwrap().history.past_len(); + crate::allocation::fail_next_for_test(); + let result = s.execute("paint.stroke", json!({"points": [[10, 10], [50, 10], [50, 50], [10, 50]], "fill": true, "color": "#ff0000"})); + let _ = crate::allocation::capacity::(0, "reset failpoint"); + let err = result.expect_err("an allocation failure must be reported, not panic").to_string(); + assert!(err.contains("filling a painted region"), "the fill reserves its buffers first: {err}"); + assert_eq!(surface(&s), pixels_before, "no pixels changed"); + assert_eq!(s.active().unwrap().history.past_len(), history_before, "no history step was recorded"); +} + #[test] fn define_brush_from_selection_survives_allocation_failure() { // The sampled tip is one f32 buffer over the whole selection, and the selection mask is a