Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/utils/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func ToDecimal(input any) (decimal.Decimal, error) {
case int64:
return decimal.New(v, 0), nil
case uint:
return decimal.New(int64(v), 0), nil
return decimal.NewFromUint64(uint64(v)), nil
case uint8:
return decimal.New(int64(v), 0), nil
case uint16:
return decimal.New(int64(v), 0), nil
case uint32:
return decimal.New(int64(v), 0), nil
case uint64:
return decimal.New(int64(v), 0), nil
return decimal.NewFromUint64(v), nil
case float64:
if !validFloat(v) {
return decimal.Decimal{}, errors.Errorf("invalid float %v, cannot convert to decimal", v)
Expand Down
15 changes: 15 additions & 0 deletions core/utils/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDecimal(t *testing.T) {
Expand Down Expand Up @@ -56,3 +57,17 @@ func TestDecimal(t *testing.T) {
}
}
}

func TestDecimalUint64NoOverflow(t *testing.T) {
t.Parallel()

// Values above math.MaxInt64 were silently corrupted: int64(math.MaxUint64) == -1.
want, _ := decimal.NewFromString("18446744073709551615") // math.MaxUint64
got, err := ToDecimal(uint64(math.MaxUint64))
require.NoError(t, err)
assert.True(t, got.Equal(want), "uint64(MaxUint64): got %s, want %s", got, want)

got, err = ToDecimal(uint(math.MaxUint64))
require.NoError(t, err)
assert.True(t, got.Equal(want), "uint(MaxUint64): got %s, want %s", got, want)
}