From 88909d6f2b86dee6969a76e1e0e3c106fe8fd20c Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 6 Feb 2023 17:55:10 +0100 Subject: [PATCH] fix(math): check for negative precision (#14922) Co-authored-by: Aleksandr Bezobchuk --- math/dec.go | 8 ++++++++ math/dec_test.go | 6 ++++++ 2 files changed, 14 insertions(+) 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) + }) +}