Skip to content

Commit

Permalink
fix(math): check for negative precision (#14922)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
julienrbrt and alexanderbez committed Feb 6, 2023
1 parent fd7bbd7 commit 88909d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down
6 changes: 6 additions & 0 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,9 @@ func TestFormatDecNonDigits(t *testing.T) {
})
}
}

func TestNegativePrecisionPanic(t *testing.T) {
require.Panics(t, func() {
math.LegacyNewDecWithPrec(10, -1)
})
}

0 comments on commit 88909d6

Please sign in to comment.