Skip to content

Commit

Permalink
types: fix nil pointer panic on NewIntFromBigInt (#9627)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7f90374)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
fedekunze authored and mergify-bot committed Dec 28, 2021
1 parent 51f7d31 commit bb46297
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

<<<<<<< HEAD
* [#10725](https://github.com/cosmos/cosmos-sdk/pull/10725) populate `ctx.ConsensusParams` for begin/end blockers.
=======
* (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt`
* (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`)
>>>>>>> 7f9037490 (types: fix nil pointer panic on `NewIntFromBigInt` (#9627))
## [v0.44.5](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.44.5) - 2021-12-02

Expand Down
7 changes: 6 additions & 1 deletion types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ func NewIntFromUint64(n uint64) Int {
return Int{b}
}

// NewIntFromBigInt constructs Int from big.Int
// NewIntFromBigInt constructs Int from big.Int. If the provided big.Int is nil,
// it returns an empty instance. This function panics if the bit length is > 256.
func NewIntFromBigInt(i *big.Int) Int {
if i == nil {
return Int{}
}

if i.BitLen() > maxBitLen {
panic("NewIntFromBigInt() out of bound")
}
Expand Down
3 changes: 3 additions & 0 deletions types/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (s *intTestSuite) TestIntPanic() {
s.Require().Panics(func() { intmax.Add(sdk.OneInt()) })
s.Require().Panics(func() { intmin.Sub(sdk.OneInt()) })

s.Require().NotPanics(func() { sdk.NewIntFromBigInt(nil) })
s.Require().True(sdk.NewIntFromBigInt(nil).IsNil())

// Division-by-zero check
s.Require().Panics(func() { i1.Quo(sdk.NewInt(0)) })

Expand Down

0 comments on commit bb46297

Please sign in to comment.