fix(math): avoid nil internal big.Int when Unmarshal receives empty i…#26536
Open
neyy91 wants to merge 2 commits into
Open
fix(math): avoid nil internal big.Int when Unmarshal receives empty i…#26536neyy91 wants to merge 2 commits into
neyy91 wants to merge 2 commits into
Conversation
Contributor
|
PR author is not in the allowed authors list. |
…nput LegacyDec/Int/Uint Unmarshal returned nil error on empty input while leaving the receiver's internal big.Int nil: the `d = nil` (resp. `i`, `u`) statement only reassigned the function-local pointer copy, not the caller's value. Methods such as IsZero/IsNegative then dereference the nil internal value and panic. This is reachable from untrusted protobuf: a DecCoin/Coin with a zero-length Amount field (valid wire bytes) decodes via Amount.Unmarshal on an empty slice, producing a nil-internal value with no error, so a later DecCoin.Validate -> IsNegative panics. Initialize the internal big.Int to zero on empty input, matching the UnmarshalJSON path. Add a regression test covering all three types.
558e332 to
ef55e5a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #26536 +/- ##
==========================================
+ Coverage 64.83% 65.06% +0.22%
==========================================
Files 784 789 +5
Lines 55096 56035 +939
==========================================
+ Hits 35724 36461 +737
- Misses 19372 19574 +202
🚀 New features to boost your workflow:
|
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.
Description
Bug
LegacyDec,IntandUintUnmarshal(math/legacy_dec.go:868,int.go:493, uint.go:198) return a nil error on
empty input while leaving the receiver's internal
big.Intnil — thed = nilstatement only reassigns the function-local pointer copy, not the caller's
value. This is asymmetric with
UnmarshalJSON, which setsd.i = new(big.Int).IsZero/IsNegativethen dereference the nil internal value and panic.Reachability (untrusted protobuf)
A
DecCoin/Coinwith a zero-lengthAmountfield is valid wire data: thegenerated decoder (coin.pb.go:596) calls
m.Amount.Unmarshal(dAtA[iNdEx:iNdEx])— an empty slice — so decode succeedswith a nil-internal Amount.
DecCoin.Validate()(dec_coin.go:140)then calls
IsNegative()with noIsNilguard → nil pointer panic.Honest encoders never emit this (
MarshalToalways writes ≥1 byte), so onlycrafted/malformed input is affected.
Fix
Initialize the internal
big.Intto zero on empty input, matchingUnmarshalJSON. Empty input now decodes to a valid zero instead of apanic-prone nil.
Test
TestUnmarshalEmptyIsZeroNotNil(all three types): before —IsZero/IsNegativepanic on the nil internal value; after — empty input is a usable zero.
TestUnmarshalZeroRoundTrip(all three types): a zero value survives aMarshal/Unmarshalround trip as a usable zero. Fullmathmodule suitegreen,
go vetclean.