From 665955d4a5c3d0971adc5122ac33b57d5511248b Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Sat, 3 Jul 2021 22:42:35 +0200 Subject: [PATCH 1/2] fix: nil pointer panic on NewIntFromBigInt --- types/int.go | 7 ++++++- types/int_test.go | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/types/int.go b/types/int.go index 0708cda58112..04b808fbc69d 100644 --- a/types/int.go +++ b/types/int.go @@ -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") } diff --git a/types/int_test.go b/types/int_test.go index 359c4859b6dc..a6ec8c6d48bd 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -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)) }) From 99b8d7125dfc63a71ee38493d05cbfb6d5c34c25 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Sat, 3 Jul 2021 22:45:14 +0200 Subject: [PATCH 2/2] c++ --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a35a077fae..62e475093860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (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`)