diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 68a0cffed1..71e3f4b9b4 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -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. diff --git a/math/uint.go b/math/uint.go index 85906047bf..be588b0cd3 100644 --- a/math/uint.go +++ b/math/uint.go @@ -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) } diff --git a/math/uint_test.go b/math/uint_test.go index 3ae815e0e6..b75b7514a5 100644 --- a/math/uint_test.go +++ b/math/uint_test.go @@ -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())