types: fix nil pointer panic on NewIntFromBigInt (#9627)

This commit is contained in:
Federico Kunze Küllmer
2021-07-05 08:36:35 -04:00
committed by GitHub
parent 57d523d29c
commit 7f9037490b
3 changed files with 10 additions and 1 deletions
+6 -1
View File
@@ -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")
}
+3
View File
@@ -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)) })