Skip to content

Fix integer overflows in feTurbulence - #1234

Closed
hsdfat wants to merge 2 commits into
linebender:mainfrom
hsdfat:fix-turbulence-overflow-1185
Closed

hsdfat wants to merge 2 commits into
linebender:mainfrom
hsdfat:fix-turbulence-overflow-1185

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 12, 2026

Copy link
Copy Markdown

Fixes #1185.

With stitchTiles="stitch" the stitch width, height and wrap values double with every octave, so they leave the i32 range after about 20 octaves and stitch.width *= 2 panics in a debug build (attempt to multiply with overflow). Release builds wrap silently.

Clamping numOctaves alone would not cover it: the same lattice code overflows in noise2, where t as i32 saturates for large coordinates and bx0 + 1 then overflows, whatever the octave count. So this makes the integer lattice arithmetic wrapping instead:

  • the stitch update uses wrapping_mul / wrapping_sub;
  • noise2 uses wrapping_add(1) for the upper lattice point and wrapping_sub when it applies the stitch.

This is what release builds already compute, so rendered output does not change. The lattice indices are masked with BM afterwards, as before. By the time the values wrap, an octave contributes far less than the color resolution; the new test checks that 25 octaves and 20 octaves give the same sample.

Tests: two unit tests in turbulence.rs (the parameters of the issue, and huge coordinates). Without the fix both panic under cargo test -p resvg --lib turbulence (debug) at the lines above; with it they pass. CI runs tests in release mode, where the overflow does not panic, so the regression is only visible in a debug run. The reproducer from the issue renders with a debug build of the CLI, and the 19 feTurbulence render tests still pass.

A clamp of numOctaves, as suggested in the issue, would still be a reasonable optimisation on top; I left it out to keep output identical.

@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.

Panic attempt to multiply with overflow in filter/turbulence.rs

2 participants