fix: reject out-of-range typed numeric conversions instead of wrapping - #357
Open
haoku123 wants to merge 1 commit into
Open
fix: reject out-of-range typed numeric conversions instead of wrapping#357haoku123 wants to merge 1 commit into
haoku123 wants to merge 1 commit into
Conversation
Every To<Int>E / To<Uint>E conversion from a typed numeric value used an unchecked Go conversion: out-of-range inputs wrapped silently and returned err == nil, while the same values arriving as strings were correctly rejected by strconv. Add range checks to the typed numeric fast paths in toNumber and toUnsignedNumber so out-of-range values fall through to the error path and the two paths agree. Fixes spf13#356
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
Fixes #356: every
To<Int>E/To<Uint>Econversion from a typed numeric value was an unchecked Go conversion. Out-of-range inputs wrapped silently and returnederr == nil, while the same values arriving as strings were correctly rejected bystrconv. The two paths disagreed.Changes
Added range checks to the typed numeric fast paths:
toNumber(signed targets): each numeric source case verifies the value fits the target before converting; out-of-range values returnok=falseand fall through to the existing error path.toUnsignedNumber(unsigned targets): same upper-bound checks added on top of the existing negative-value rejection.numberFitsFromInt64,numberFitsFromUint64,numberFitsFromFloat64:NaN/±Inffloats,2^63/2^64boundaries for 64-bit targets (float64 cannot representMaxInt64),-128.9 → -128),int/uintportable viastrconv.IntSize,Verification
Repro from the issue now errors for every case:
42,3.7 → 3,-128.9 → -128,uint64(7).TestTypedNumericOverflow(11 overflow cases) +TestTypedNumericInRange.go test -count=1 ./...PASS,go vet ./...clean.