Repository navigation
fix: reject a second sign when parsing a long number - #54
Merged
Merged
Conversation
Parse reads up to 41 characters with the u128 code and longer strings through
big.Int. The big.Int path cut off a leading '-' and handed the rest to
big.Int.SetString, which takes a sign of its own, so "-+" in front of a long
number was a number: Parse("-+1234567890123456789012345678901234567890123456")
returned -1234567890123456789012345678901234567890123456 and no error, while
"-+123", "+-..." and "--..." are errors. UnmarshalJSON, UnmarshalText and Scan
read through the same code.
The first character after the sign has to be a digit now. That covers the
"-.123" and "+.123" the check was for before.
TestParse has the new cases, two of them fail on master.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #54 +/- ##
=======================================
Coverage 96.72% 96.72%
=======================================
Files 5 5
Lines 1529 1529
=======================================
Hits 1479 1479
Misses 27 27
Partials 23 23 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
quagmt
approved these changes
Nov 20, 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.
Parsereads strings of up to 41 characters with the u128 code and longer ones throughbig.Int. Thebig.Intpath cuts off a leading-and hands the rest tobig.Int.SetString, which takes a sign of its own, so-+in front of a long number was accepted:UnmarshalJSON,UnmarshalTextandScango through the same code, so a JSON string like"-+1234567890123456789012345678901234567890123456"was a valid number too.The first character after the sign has to be a digit now (it was a check for
.only), which keeps-.5and+.5rejected as before.I compared the short and the long path with 200,000 random strings (signs, fractions, up to 19 digits on each side), once as they are and once padded with zeros to more than 41 characters: the results are the same, this is the only difference.
TestParsehas the new cases, two of them fail on mastergo test ./...passes,gofmtandgo vetare clean (golangci-lintis not installed here)