Parse full-range hex literals with strtoull#1233
Open
cuiweixie wants to merge 1 commit into
Open
Conversation
Hex literals like 0xFFFFFFFFFFFFFFFF exceed signed long long; strtoll overflows and errno becomes ERANGE, incorrectly rejecting the literal. Use strtoull and cast to double. Add a regression test for UINT64_MAX.
9f37a7c to
4a7f1fa
Compare
Contributor
|
As an alternative, in PR #984 I've attempted to create an entirely custom number parsing method. Which, should handle this case, but is certainly a much more aggressive change to the language and handles this a completely different way. It'd consume as many digits that'd fit in the mantissa of a double (about what would fit in 52 bits), consume the rest ignoring the value but incrementing an exponent, then multiplies that mantissa value (as a long double) by the power of the base raised to the exponent. Which should also prevent the overflow as we only parse enough to fit inside of about 52-bits of a 64-bit integer. |
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
Hex integer literals are tokenized as
0x...and parsed with the C library.strtollonly covers the signed range;0xFFFFFFFFFFFFFFFF(UINT64_MAX) overflows, setsERANGE, and the lexer incorrectly reported "Number literal was too large".Use
strtoullso the full 64-bit unsigned range parses, then cast todouble.Regression
test/core/number/hex_literal_uint64_max.wren: fails withstrtoll(compile error), passes withstrtoull.Testing