Repository navigation
Format: fixes integer wraparound when parsing numbers - #2696
Merged
Merged
Conversation
Numbers that don't fit in 32 bits were truncated, so `{4294967297}` was read
as `{1}`, `{4294967296}` as the next argument, and `{$4294967297}` as the
first constant. The same applied to the start, end and length of `~`, `:`, `<`,
`>` and `|`. Clamp them instead, so that they stay out of range.
Also avoid negating INT32_MIN for the ellipsis length of `{1:-2147483648}`,
which is a signed integer overflow.
1 task done
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.
Summary
Numbers in a format string that don't fit in 32 bits were truncated, so they could turn into a valid value:
{4294967297}(2 arguments){4294967296}{}){$4294967297}{1~4294967297}on12345 678902345 67890(read as{1~1}){1:-4294967296}on12345 67890{1:0})12345 67890The causes were
(uint32_t) strtoul(...)ingetArgumentIndex()and(int32_t) strtol(...)for the constant index and for the start, end and length of~,:,<,>and|. On 64-bit platformslongis wider than the type it was cast to.There is also a signed overflow: for a negative length,
{1:-2147483648}negatesINT32_MINto get the ellipsis length. UBSan reports it:Related issue (required for new logos for new distros)
Not a logo. No issue, it is a small fix in the same code as #2669.
Changes
src/common/impl/format.c: newstrToInt32()that clamps instead of wrapping, used for the constant index, the substring start and end and the length.getArgumentIndex()compares theunsigned longbefore narrowing.INT32_MINis no longer negated.tests/format.c: cases for each of the above. The first one fails on the old code.CHANGELOG.md: bugfix entry.Screenshots
Not a visual change.
Checklist
Built with
-DBUILD_TESTS=On -DENABLE_ASAN=Onand-fsanitize=undefined; all 19 tests pass without sanitizer reports.