From 1565db93887d9bd20a2ce23434f1cec1406a4f69 Mon Sep 17 00:00:00 2001 From: simonkundrik <117163790+simonkundrik@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:44:44 +0000 Subject: [PATCH] fix: reject a second sign when parsing a long number 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. --- bint.go | 4 ++-- decimal_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bint.go b/bint.go index 956a842..baf2f97 100644 --- a/bint.go +++ b/bint.go @@ -144,8 +144,8 @@ func parseBint(s []byte) (bool, bint, uint8, error) { return false, bint{}, 0, errInvalidFormat(s) } - // prevent "-.123" or "+.123" - if s[pos] == '.' { + // prevent "-.123" or "+.123", and a second sign as in "-+123" that big.Int would read as a sign again + if s[pos] < '0' || s[pos] > '9' { return false, bint{}, 0, errInvalidFormat(s) } diff --git a/decimal_test.go b/decimal_test.go index 75a467b..80ccb5c 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -131,6 +131,12 @@ func TestParse(t *testing.T) { {"+340282366920938463463374607431768211459", "340282366920938463463374607431768211459", nil}, {"340282366920938463463374607431768211459.", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.'", ErrInvalidFormat)}, {"--340282366920938463463374607431768211459", "", fmt.Errorf("%w: can't parse '--340282366920938463463374607431768211459'", ErrInvalidFormat)}, + {"-+1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '-+1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"-+340282366920938463463374607431768211459.123", "", fmt.Errorf("%w: can't parse '-+340282366920938463463374607431768211459.123'", ErrInvalidFormat)}, + {"+-1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '+-1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"++1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '++1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"--1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '--1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"-+123", "", fmt.Errorf("%w: can't parse '-+123'", ErrInvalidFormat)}, {".1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, {"+.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '+.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, {"-.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '-.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)},