From 6e9731d50e379f33d28ef96ddcebcf1314c3f1f3 Mon Sep 17 00:00:00 2001 From: skyjerry Date: Tue, 12 Mar 2024 22:17:08 +0800 Subject: [PATCH] Title: Fix ToUint64E function in cast library for Go 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. --- cast_test.go | 6 ++++++ caste.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cast_test.go b/cast_test.go index 6bea164..2282098 100644 --- a/cast_test.go +++ b/cast_test.go @@ -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, diff --git a/caste.go b/caste.go index d49bbf8..618adc9 100644 --- a/caste.go +++ b/caste.go @@ -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: