Skip to content

Fix filter3x3 panic on zero-dimension images#3055

Closed
chatman-media wants to merge 1 commit into
image-rs:mainfrom
chatman-media:fix/filter3x3-zero-dimension-overflow
Closed

Fix filter3x3 panic on zero-dimension images#3055
chatman-media wants to merge 1 commit into
image-rs:mainfrom
chatman-media:fix/filter3x3-zero-dimension-overflow

Conversation

@chatman-media

Copy link
Copy Markdown

Closes #3026.

imageops::filter3x3 loops over 1..width - 1 / 1..height - 1. When an image has a zero width or height, these subtractions underflow, panicking with "attempt to subtract with overflow" (debug) or wrapping to a near-u32::MAX range (release).

Switched both bounds to saturating_sub(1) so a zero dimension yields an empty range, matching the existing behavior for a dimension of 1 (no interior pixels, output left as-is). Added a regression test covering the 0 and 1 dimension cases.

Note: #3028 (the unsharpen/gaussian-blur path) is handled separately by #3007; this only touches the independent filter3x3 code path.

filter3x3 iterated 1..width-1 / 1..height-1, which underflows when an
image has a zero width or height, panicking with a subtract overflow
(or wrapping to a huge range in release). Use saturating_sub so a
dimension of 0 yields an empty range, matching the existing behavior for
images with a dimension of 1 (no interior pixels, output left untouched).

Closes image-rs#3026
@197g

197g commented Jun 26, 2026

Copy link
Copy Markdown
Member

The good thing is this allows comparison with the alternative approach in #2874 . However, the other looks like a much more principled choice, rather than merely avoiding the fault it provides a fix to the input cases that improves the specification of the function.

@chatman-media

Copy link
Copy Markdown
Author

Closing in favor of #2874 — that takes the more principled route by fixing the abyss policy rather than just guarding the panic. Thanks for the comparison!

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 in filter3x3() on zero-dimension image due to unsigned integer underflow

2 participants