fix(math): check for negative precision (#14922)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Julien Robert 2023-02-06 17:55:10 +01:00 committed by GitHub
parent fd7bbd7e8d
commit 88909d6f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -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))
}

View File

@ -714,3 +714,9 @@ func TestFormatDecNonDigits(t *testing.T) {
})
}
}
func TestNegativePrecisionPanic(t *testing.T) {
require.Panics(t, func() {
math.LegacyNewDecWithPrec(10, -1)
})
}