diff --git a/math/dec.go b/math/dec.go index 809a8e8616..3123f8d7ae 100644 --- a/math/dec.go +++ b/math/dec.go @@ -143,19 +143,15 @@ func LegacyNewDecFromIntWithPrec(i Int, prec int64) LegacyDec { // // CONTRACT - This function does not mutate the input str. func LegacyNewDecFromStr(str string) (LegacyDec, error) { - if len(str) == 0 { - return LegacyDec{}, fmt.Errorf("%s: %w", str, ErrLegacyEmptyDecimalStr) - } - // first extract any negative symbol neg := false - if str[0] == '-' { + if len(str) > 0 && str[0] == '-' { neg = true str = str[1:] } if len(str) == 0 { - return LegacyDec{}, fmt.Errorf("%s: %w", str, ErrLegacyEmptyDecimalStr) + return LegacyDec{}, ErrLegacyEmptyDecimalStr } strs := strings.Split(str, ".") diff --git a/math/fuzz_test.go b/math/fuzz_test.go new file mode 100644 index 0000000000..3fae5a7e5c --- /dev/null +++ b/math/fuzz_test.go @@ -0,0 +1,30 @@ +package math + +import ( + "testing" +) + +func FuzzLegacyNewDecFromStr(f *testing.F) { + if testing.Short() { + f.Skip("running in -short mode") + } + + f.Add("-123.456") + f.Add("123.456789") + f.Add("123456789") + f.Add("0.12123456789") + f.Add("-12123456789") + + f.Fuzz(func(t *testing.T, input string) { + dec, err := LegacyNewDecFromStr(input) + if err != nil && !dec.IsNil() { + t.Fatalf("Inconsistency: dec.notNil=%v yet err=%v", dec, err) + } + }) +} + +func TestDecNegativePrecision(t *testing.T) { + t.Skip("https://github.com/cosmos/cosmos-sdk/issues/14004 is not yet addressed") + + LegacyNewDecWithPrec(10, -1) +}