Skip to content

Commit

Permalink
Title: Fix ToUint64E function in cast library for Go
Browse files Browse the repository at this point in the history
Detail:
The ToUint6E4 function in the Go cast library was fixed to correctly handle strings that overflow the maximum value of int64.

Previously, the function incorrectly returned zero for these input values because it was using strconv.ParseInt internally, which doesn't handle values greater than the max int64. The fix replaces the use of strconv.ParseInt with strconv.ParseUint.
  • Loading branch information
skyjerry committed Mar 12, 2024
1 parent 48ddde5 commit 6e9731d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func TestToUintE(t *testing.T) {
func TestToUint64E(t *testing.T) {
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))

// Maximum value of uint64
tests = append(tests,
testStep{"18446744073709551615", uint64(18446744073709551615), false},
testStep{"18446744073709551616", uint64(0), true},
)

runNumberTest(
qt.New(t),
tests,
Expand Down
4 changes: 2 additions & 2 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,12 @@ func ToUint64E(i interface{}) (uint64, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint64(v), nil
return v, nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
case json.Number:
Expand Down

0 comments on commit 6e9731d

Please sign in to comment.