fix(gif): avoid int32 overflow in palette-split pixel-count math#5292
Merged
lgritz merged 3 commits intoJul 6, 2026
Conversation
GifSplitPalette() computed `numPixels * (splitElt - firstElt)` as a 32-bit int before dividing. For any image over roughly 16.9M pixels (e.g. 4117x4117), that intermediate multiplication overflows INT_MAX and is undefined behavior; in practice it produced a bogus negative/garbage split count that could drive later indexing into `image` out of its bounds. Do the multiply in 64 bits and narrow back afterward -- the quotient is always <= numPixels, so the narrowing itself is safe. Assisted-by: Claude Code / Sonnet 5 Signed-off-by: Larry Gritz <lg@larrygritz.com>
Contributor
|
Given the max dimensions of 32768x32768x4, there are 4 other int overflows related to gif input, might as well do them in the same PR:
For
|
Signed-off-by: Larry Gritz <lg@larrygritz.com>
Collaborator
Author
|
So amended. |
jessey-git
approved these changes
Jul 5, 2026
jessey-git
left a comment
Contributor
There was a problem hiding this comment.
Looks good, but watch out for clang-format.
Signed-off-by: Larry Gritz <lg@larrygritz.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GifSplitPalette() computed
numPixels * (splitElt - firstElt)as a 32-bit int before dividing. For any image over roughly 16.9M pixels (e.g. 4117x4117), that intermediate multiplication overflows INT_MAX and is undefined behavior; in practice it produced a bogus negative/garbage split count that could drive later indexing intoimageout of its bounds.Do the multiply in 64 bits and narrow back afterward -- the quotient is always <= numPixels, so the narrowing itself is safe.
Assisted-by: Claude Code / Sonnet 5