Skip to content

Commit

Permalink
fix(types): dec from string must be finite (#1590)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
aleem1314 and ryanchristo committed Nov 7, 2022
1 parent e06a36b commit 7c9e329
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion types/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
ErrInvalidDecString = errors.Register(mathCodespace, 1, "invalid decimal string")
ErrUnexpectedRounding = errors.Register(mathCodespace, 2, "unexpected rounding")
ErrNonIntegeral = errors.Register(mathCodespace, 3, "value is non-integral")
ErrInfiniteString = errors.Register(mathCodespace, 4, "value is infinite")
)

// In cosmos-sdk#7773, decimal128 (with 34 digits of precision) was suggested for performing
Expand All @@ -54,7 +55,13 @@ func NewDecFromString(s string) (Dec, error) {
if err != nil {
return Dec{}, ErrInvalidDecString.Wrap(err.Error())
}
return Dec{*d}, nil

d1 := Dec{*d}
if d1.dec.Form == apd.Infinite {
return d1, ErrInfiniteString.Wrapf(s)
}

return d1, nil
}

func NewNonNegativeDecFromString(s string) (Dec, error) {
Expand Down
6 changes: 6 additions & 0 deletions types/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,9 @@ func TestToSdkInt(t *testing.T) {
require.Equal(t, tc.out, b.String(), "test_%d", idx)
}
}

func TestInfDecString(t *testing.T) {
_, err := NewDecFromString("iNf")
require.Error(t, err)
require.ErrorIs(t, err, ErrInfiniteString)
}

0 comments on commit 7c9e329

Please sign in to comment.