diff --git a/CHANGELOG.md b/CHANGELOG.md index f3ff8b6076bc..47bbafb6d6c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -186,6 +186,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa ### Bug Fixes +* (types) [\#7084](https://github.com/cosmos/cosmos-sdk/pull/7084) Fix panic when calling `BigInt()` on an uninitialized `Int`. * (x/bank) [\#6536](https://github.com/cosmos/cosmos-sdk/pull/6536) Fix bug in `WriteGeneratedTxResponse` function used by multiple REST endpoints. Now it writes a Tx in StdTx format. * (x/staking) [\#6529](https://github.com/cosmos/cosmos-sdk/pull/6529) Export validator addresses (previously was empty). diff --git a/types/decimal.go b/types/decimal.go index 3dff15351a94..a6ef29a03c00 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -208,6 +208,10 @@ func (d Dec) Abs() Dec { return Dec{new(big.Int).Abs(d.i)} } // absolut // BigInt returns a copy of the underlying big.Int. func (d Dec) BigInt() *big.Int { + if d.IsNil() { + return nil + } + copy := new(big.Int) return copy.Set(d.i) } diff --git a/types/int.go b/types/int.go index 4326a3ee9931..81f6b1c04ddb 100644 --- a/types/int.go +++ b/types/int.go @@ -76,9 +76,17 @@ type Int struct { // BigInt converts Int to big.Int func (i Int) BigInt() *big.Int { + if i.IsNil() { + return nil + } return new(big.Int).Set(i.i) } +// IsNil returns true if Int is uninitialized +func (i Int) IsNil() bool { + return i.i == nil +} + // NewInt constructs Int from int64 func NewInt(n int64) Int { return Int{big.NewInt(n)} diff --git a/types/int_test.go b/types/int_test.go index aa55ee659ebd..a4866a97b90d 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -81,6 +81,8 @@ func TestIntPanic(t *testing.T) { // Division-by-zero check require.Panics(t, func() { i1.Quo(NewInt(0)) }) + + require.NotPanics(t, func() { Int{}.BigInt() }) } // Tests below uses randomness