fix Improve error handling for hex value overflow #1999
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.



Fix error handling for hex value decoding in Long type. In general, when parsing an integer into a wider, unsigned type (here
uint64) and then converting it to a narrower or signed type (int64), you must ensure the parsed value is within the target type’s range before conversion. For signed 64-bit (int64), that means checking that theuint64value is not greater thanmath.MaxInt64. If the value is out of range, you should return an error instead of performing the unsafe cast.For this specific case, the best minimal fix is to add an explicit range check in
(*Long).UnmarshalGraphQLbefore converting theuint64returned byhexutil.DecodeUint64intoLong(which isint64). Ifvalue > math.MaxInt64,UnmarshalGraphQLshould return an error indicating that the hex long is out of range forLong. This preserves existing behavior for all in-range values and only changes behavior when the user provides an out-of-range hex value, which currently would silently wrap.Concretely:
graphql/graphql.go, insideUnmarshalGraphQL, in thecase string:/if strings.HasPrefix(input, "0x")branch, aftervalue, err := hexutil.DecodeUint64(input)and before*b = Long(value), add:if value > math.MaxInt64 { return fmt.Errorf("hex value %s overflows Long", input) }.math.MaxInt64, add an import of the standard library packagemathtographql/graphql.go.common/hexutil/hexutil.go, since it already correctly parses intouint64; the unsafe step is ingraphql/graphql.go.Changes
If a string is parsed into an int using strconv.Atoi, and subsequently that int is converted into another integer type of a smaller size, the result can produce unexpected values. This also applies to the results of strconv.ParseInt and strconv.ParseUint when the specified size is larger than the size of the type that number is converted to.
Checklist
Cross repository changes
Testing
References
Wikipedia Integer overflow
Go language specification Integer overflow
Documentation for strconv.Atoi
Documentation for strconv.ParseInt
Documentation for strconv.ParseUint