Fix #1543: oversized Satin and Bevel & Emboss sizes render instead of panicking (#1563)

A Satin size or a Bevel & Emboss size or softness of 1e308 reached the
box blur unclamped: the kernel radius saturated to ~2^62 taps and the
render panicked with "capacity overflow"; 1e6 ran for minutes. Drop
Shadow survived because its reach is clamped in margin(). The blur width
is now capped at MAX_REACH (and NaN is 1) in one place, box_width, which
both the CPU tent and the GPU's tent_kernel go through, so values from
commands and from files are bounded alike.


Claude-Session: https://claude.ai/code/session_01UHgFEHEsefoQoBParH47JA

Co-authored-by: Claude <[email protected]>
This commit is contained in:
BFlatandClaude authored and GitHub committed 2026-10-09 09:47:13 +07:00
1 parent 47ad748638
commit f82a55f056
2 files changed
+37 -3

No files matched your search

+16 -3
View File
@@ -706,10 +706,17 @@ fn paint_glow(dst: &mut Buffer, m: &Map, g: &Glow, shape_bounds: Rect, anchor: (
paint(dst, &strength, |i| prepared.sample(1.0 - m.v.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0)), g.common.blend, g.common.opacity);
}
/// A box width as the blurs use it: at least 1, at most [`MAX_REACH`] (an effect never reaches
/// further, see [`margin`]), and 1 for NaN. A size or softness from a command or a file can be
/// anything, and an unbounded width sizes the kernel and its buffers (#1543).
fn box_width(w: f32) -> f32 {
if w.is_nan() { 1.0 } else { w.clamp(1.0, MAX_REACH) }
}
/// Normalised weights of a centred box of (fractional) width `w`: tap `i` gets the overlap of
/// `[i - 0.5, i + 0.5]` with `[-w/2, w/2]`.
fn box_weights(w: f32) -> Vec<f32> {
let w = w.max(1.0);
let w = box_width(w);
let half = w / 2.0;
let r = (half - 0.5).ceil().max(0.0) as i64;
let v: Vec<f32> = (-r..=r).map(|i| ((i as f32 + 0.5).min(half) - (i as f32 - 0.5).max(-half)).max(0.0)).collect();
@@ -735,10 +742,11 @@ pub fn tent_kernel(w: f32) -> (i32, Vec<f32>) {
/// Box geometry for a (fractional) width: (`r`, end-tap weight `f`, 1 / width) — the
/// [`box_weights`] taps are `r - 1` full ones each side of the centre plus the two end taps at `f`.
fn box_geom(bw: f32) -> (i64, f64, f64) {
let half = bw.max(1.0) / 2.0;
let bw = box_width(bw);
let half = bw / 2.0;
let r = (half - 0.5).ceil().max(0.0) as i64;
let f = f64::from((half - (r as f32 - 0.5)).clamp(0.0, 1.0));
(r, f, 1.0 / f64::from(bw.max(1.0)))
(r, f, 1.0 / f64::from(bw))
}
/// One box pass over `src` (zero outside it) evaluated at `x0 .. x0 + dst.len()`, as a running
@@ -1691,6 +1699,11 @@ mod tests {
assert_eq!(r, 4);
assert!(k[0] > 0.0 && k[0] < 1.0 / 25.0);
assert_eq!(tent_kernel(1.0), (0, vec![1.0]));
// #1543: a huge, infinite or NaN width is capped, not turned into a kernel of 2^62 taps.
for w in [1e30, f32::INFINITY, f32::MAX] {
assert_eq!(tent_kernel(w), tent_kernel(MAX_REACH), "{w}");
}
assert_eq!(tent_kernel(f32::NAN), (0, vec![1.0]));
}
fn no_tex() -> TextureCtx<'static> {
+21
View File
@@ -753,6 +753,27 @@ mod tests {
}
}
/// #1543: a Satin or Bevel & Emboss size or softness far past Photoshop's range rendered a
/// blur kernel of 2^62 taps ("capacity overflow"), or ran for minutes at 1e6. The blur width
/// is capped at the effect reach, so the document still renders, quickly.
#[test]
fn oversized_satin_and_bevel_sizes_render() {
for (cmd, p) in [
("satin", json!({"size": 1e308})),
("satin", json!({"size": 1_000_000})),
("bevelEmboss", json!({"size": 1e308})),
("bevelEmboss", json!({"soften": 1e308})),
("bevelEmboss", json!({"size": 1_000_000, "soften": 1_000_000})),
] {
let mut s = session();
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
s.execute(&format!("layer.layerStyle.{cmd}"), p.clone()).unwrap();
let start = std::time::Instant::now();
s.execute("document.pixel", json!({"x": 0, "y": 0})).unwrap_or_else(|e| panic!("{cmd} {p}: {e}"));
assert!(start.elapsed().as_secs() < 20, "{cmd} {p}: {:?}", start.elapsed());
}
}
#[test]
fn params_are_applied_and_replace_or_add() {
let mut s = session();