Fix integer overflow#201
Open
pennam wants to merge 3 commits into
Open
Conversation
Chunk size is accumulated from hex digits with no bounds check, so a long enough size overflows iChunkLength (int) - UB, yielding a bogus/negative length. Guard the accumulation with an INT_MAX pre-check so it can never overflow.
read() decremented iChunkLength unconditionally; if it hit 0 mid-chunk it went negative (UB at INT_MIN). Only decrement while it is still positive.
The old guard computed value*10+digit first, then checked for wrap-around - but that computation is itself UB and the check may be optimised away. Use a LONG_MAX pre-check so the overflow never happens.
|
Memory usage change @ c762f8d
Click for full report table
Click for full report CSV |
|
|
||
| iChunkLength = (iChunkLength * 16) + strtol(digit, NULL, 16); | ||
| // Only apply if the value fits in int without overflow. | ||
| if (iChunkLength <= ((INT_MAX - digitValue) / 16)) { |
There was a problem hiding this comment.
This is equivalent, but I think it may be easier to read
Suggested change
| if (iChunkLength <= ((INT_MAX - digitValue) / 16)) { | |
| if ((iChunkLength * 16) + digitValue <= INT_MAX) { |
| iContentLength = _iContentLength; | ||
| int digitValue = c - '0'; | ||
| // Only apply if the value fits in long without overflow. | ||
| if (iContentLength <= ((LONG_MAX - digitValue) / 10)) { |
There was a problem hiding this comment.
This is equivalent, but I think it may be easier to read
Suggested change
| if (iContentLength <= ((LONG_MAX - digitValue) / 10)) { | |
| if ((iContentLength * 10) + digitValue <= LONG_MAX) { |
andreagilardoni
approved these changes
Jul 13, 2026
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.
Chunk size is accumulated from hex digits with no bounds check, so a long
enough size overflows iChunkLength (int) - UB, yielding a bogus/negative
length. Guard the accumulation with an INT_MAX pre-check so it can never
overflow.