Skip to content

Commit

Permalink
fix(math): preventative ciel call (#18519)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Nov 20, 2023
1 parent e7b1d10 commit bed9520
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j

* [#18421](https://github.com/cosmos/cosmos-sdk/pull/18421) Add mutative api for `LegacyDec.BigInt()`.

### Bug Fixes

* [#18519](https://github.com/cosmos/cosmos-sdk/pull/18519) Prevent Overflow in `Dec.Ceil()`.

## [math/v1.2.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.2.0) - 2023-11-07

### Features
Expand Down
4 changes: 4 additions & 0 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,10 @@ func (d LegacyDec) Ceil() LegacyDec {
return LegacyNewDecFromBigInt(quo)
}

if d.i.BitLen() >= maxDecBitLen {
panic("Int overflow")
}

return LegacyNewDecFromBigInt(quo.Add(quo, oneInt))
}

Expand Down
8 changes: 8 additions & 0 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,14 @@ func (s *decimalTestSuite) TestDecCeil() {
}
}

func (s *decimalTestSuite) TestCeilOverflow() {
d, err := math.LegacyNewDecFromStr("66749594872528440074844428317798503581334516323645399060845050244444366430645.000000000000000001")
s.Require().NoError(err)
s.Require().True(d.BigInt().BitLen() <= 315, "d is too large")
// this call panics because the value is too large
s.Require().Panics(func() { d.Ceil() }, "Ceil should panic on overflow")
}

func (s *decimalTestSuite) TestPower() {
testCases := []struct {
input math.LegacyDec
Expand Down

0 comments on commit bed9520

Please sign in to comment.