diff --git a/math/dec.go b/math/dec.go index 3123f8d7ae..844b3039af 100644 --- a/math/dec.go +++ b/math/dec.go @@ -69,6 +69,10 @@ func LegacySmallestDec() LegacyDec { return LegacyDec{new(big.Int).Set(oneInt)} // calculate the precision multiplier func calcPrecisionMultiplier(prec int64) *big.Int { + if prec < 0 { + panic(fmt.Sprintf("negative precision %v", prec)) + } + if prec > LegacyPrecision { panic(fmt.Sprintf("too much precision, maximum %v, provided %v", LegacyPrecision, prec)) } @@ -79,6 +83,10 @@ func calcPrecisionMultiplier(prec int64) *big.Int { // get the precision multiplier, do not mutate result func precisionMultiplier(prec int64) *big.Int { + if prec < 0 { + panic(fmt.Sprintf("negative precision %v", prec)) + } + if prec > LegacyPrecision { panic(fmt.Sprintf("too much precision, maximum %v, provided %v", LegacyPrecision, prec)) } diff --git a/math/dec_test.go b/math/dec_test.go index a58ba7769d..ae897c3ba8 100644 --- a/math/dec_test.go +++ b/math/dec_test.go @@ -714,3 +714,9 @@ func TestFormatDecNonDigits(t *testing.T) { }) } } + +func TestNegativePrecisionPanic(t *testing.T) { + require.Panics(t, func() { + math.LegacyNewDecWithPrec(10, -1) + }) +}