Repository navigation
fix: prevent silent wraparound in out-of-range numeric conversions - #368
Closed
AdamMagued wants to merge 1 commit into
Closed
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
Add range bounds checking to toNumber and toUnsignedNumber before converting typed numeric values (e.g. uint64, float64). Previously, unchecked Go conversions directly evaluated T(s), causing out-of-range values such as uint64(math.MaxUint64) to wrap to -1 and float64(1e300) or NaN to yield math.MinInt64 with a nil error, inconsistent with string inputs which correctly returned range errors. Introduce helper functions numberFitsFromInt64, numberFitsFromUint64, and numberFitsFromFloat64 to ensure values fit the target numeric type prior to conversion, returning an error when out-of-range. Add regression tests for typed numeric overflow and NaN conversions. Fixes spf13#356
|
|
Author
|
Closing in favor of earlier community PR #357 to avoid duplicate review effort. |
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.
Summary
Fixes #356
When converting typed numeric inputs (such as uint64(math.MaxUint64) or float64(1e300)), toNumber[T] directly executed unchecked Go conversions T(s) without verifying if the value fits the target representation. As a result:
Changes
Verification