Skip to content

Commit

Permalink
Merge PR #7084: types: fix panic on Int.BigInt()
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored Aug 18, 2020
1 parent 740a4c5 commit 7f0c3f0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
2 changes: 2 additions & 0 deletions types/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f0c3f0

Please sign in to comment.