Skip to content

Commit

Permalink
fix(math): fix panic on Uint.BigInt() (#18228)
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu authored Oct 24, 2023
1 parent 2f23e8a commit ff3ec25
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j

### Bug Fixes

* [#18228](https://github.com/cosmos/cosmos-sdk/pull/18228) Fix panic when calling `BigInt()` on an uninitialized `Uint`.
* [#18214](https://github.com/cosmos/cosmos-sdk/pull/18214) Ensure that modifying the argument to `NewUIntFromBigInt` doesn't mutate the returned value.
* [#18211](https://github.com/cosmos/cosmos-sdk/pull/18211) RelativePow now returns 1 when 0^0, before it was returning the scale factor.
* [#17725](https://github.com/cosmos/cosmos-sdk/pull/17725) Fix state break in ApproxRoot. This has been present since math/v1.0.1. It changed the rounding behavior at precision end in an intermediary division from banker's to truncation. The truncation occurs from binary right shift in the case of square roots. The change is now reverted back to banker's rounding universally for any root.
Expand Down
3 changes: 3 additions & 0 deletions math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Uint struct {

// BigInt converts Uint to big.Int
func (u Uint) BigInt() *big.Int {
if u.IsNil() {
return nil
}
return new(big.Int).Set(u.i)
}

Expand Down
2 changes: 2 additions & 0 deletions math/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (s *uintTestSuite) TestUintPanics() {
s.Require().Panics(func() { uintmin.Sub(sdkmath.OneUint()) })
s.Require().Panics(func() { uintmin.Decr() })

s.Require().NotPanics(func() { sdkmath.Uint{}.BigInt() })

s.Require().Equal(uint64(0), sdkmath.MinUint(sdkmath.ZeroUint(), sdkmath.OneUint()).Uint64())
s.Require().Equal(uint64(1), sdkmath.MaxUint(sdkmath.ZeroUint(), sdkmath.OneUint()).Uint64())

Expand Down

0 comments on commit ff3ec25

Please sign in to comment.