fix(math): preventative ciel call (#18519)

This commit is contained in:
Marko 2023-11-20 11:28:15 +01:00 committed by GitHub
parent e7b1d10998
commit bed952022b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

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

View File

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

View File

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