Skip to content

fix: reject a second sign when parsing a long number - #54

Merged
quagmt merged 1 commit into
quagmt:masterfrom
simonkundrik:fix/parse-second-sign
Nov 20, 2026
Merged

quagmt merged 1 commit into
quagmt:masterfrom
simonkundrik:fix/parse-second-sign

Conversation

@simonkundrik

Copy link
Copy Markdown
Contributor

Parse reads strings of up to 41 characters with the u128 code and longer ones through big.Int. The big.Int path cuts off a leading - and hands the rest to big.Int.SetString, which takes a sign of its own, so -+ in front of a long number was accepted:

udecimal.Parse("-+1234567890123456789012345678901234567890123456") // -1234567890123456789012345678901234567890123456, no error
udecimal.Parse("-+123")                                            // error
udecimal.Parse("--1234567890123456789012345678901234567890123456") // error

UnmarshalJSON, UnmarshalText and Scan go 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 -.5 and +.5 rejected 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.

  • TestParse has the new cases, two of them fail on master
  • go test ./... passes, gofmt and go vet are clean (golangci-lint is not installed here)

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

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.72%. Comparing base (ba44fa2) to head (1565db9).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@quagmt
quagmt merged commit cc65de1 into quagmt:master Nov 20, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants