Skip to content

Fix an integer overflow in the box blur for a huge stdDeviation - #1260

Closed
abdymazhit wants to merge 1 commit into
linebender:mainfrom
abdymazhit:fix/box-blur-huge-std-dev
Closed

abdymazhit wants to merge 1 commit into
linebender:mainfrom
abdymazhit:fix/box-blur-huge-std-dev

Conversation

@abdymazhit

Copy link
Copy Markdown

Fixes #1258.

create_box_gauss converts the ideal box width to i32, so for a σ above roughly 1.4·10⁹ device pixels it saturates to i32::MAX, and wl + 2 overflows. With overflow checks this panics; in release builds the box size wraps. feGaussianBlur and feDropShadow both reach it.

Changes:

  • The upper box size wu saturates, so the box sizes stay valid odd widths.
  • box_blur::apply leaves the image transparent, without blurring, when a box is wider than 4 * 255 times the blurred length (the width for horizontal boxes, the height for vertical ones). Pixels past the edges are transparent, so such a box sums at most 255 * len per channel and every average is below a quarter. That rounds to zero, and the passes after it blur a transparent image. Where the blur did not overflow before, no pixel changes.
  • The shortcut also keeps radii of about 10⁹ pixels out of blur_radius * width, which overflows usize on 32-bit targets.

Tests: three unit tests in box_blur.rs:

  • box sizes for σ of 1.5·10⁹, 4.1·10¹⁸, f32::MAX and infinity;
  • a blur of 4.1·10¹⁸ leaves an opaque 16×16 image transparent;
  • σ = 10 000, whose boxes are just narrower than the skipped width, already leaves that image transparent, so the shortcut changes no pixel.

Without the fix, the first two panic at box_blur.rs:48 under cargo test -p resvg --lib box_blur. cargo test --all --release passes, and both SVGs from the issue render with a debug build of the CLI.

#1255 moves filter rendering to filtra, which bounds σ with MAX_STD_DEV. This fixes the current renderer until then.

🤖 Generated with Claude Code

create_box_gauss converts the ideal box width to i32, so for a sigma above
roughly 1.4e9 device pixels it saturates to i32::MAX and `wl + 2` overflows:
a panic with overflow checks, a wrapped box size in release builds. Both
feGaussianBlur and feDropShadow reach it.

The upper box size now saturates, and apply leaves the image transparent
when a box is wider than 4 * 255 times the blurred length. Pixels past the
edges are transparent, so such a box averages every channel below a
quarter, which rounds to zero, and the passes after it blur a transparent
image: no pixel changes where the blur did not overflow before.

Fixes linebender#1258.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LaurenzV

Copy link
Copy Markdown
Collaborator

Superseded by #1255, thanks regardless!

@LaurenzV LaurenzV closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_box_gauss overflows i32 for a huge stdDeviation (panics with overflow checks)

2 participants