From 1089f715fc73d861599a55a62725293b2b6e4761 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 21 Aug 2023 11:20:30 +0200 Subject: [PATCH] fix(math): fix panic in `.Size()` (#17480) --- math/CHANGELOG.md | 10 ++++++++-- math/int.go | 4 ++++ math/int_test.go | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index d742b8e4abe4..54c0ba994cb3 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -34,7 +34,13 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j # Changelog -## [math/v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.1.0) - 2023-08-18 +## [math/v1.1.1](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.1.1) - 2023-08-21 + +### Bug Fixes + +* [#17480](https://github.com/cosmos/cosmos-sdk/pull/17480) Fix panic when calling `.Size()` on a nil `math.Int` value. + +## [math/v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.1.0) - 2023-08-19 ### Features @@ -48,7 +54,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j ### Bug Fixes * [#17352](https://github.com/cosmos/cosmos-sdk/pull/17352) Ensure that modifying the argument to `NewIntFromBigInt` doesn't mutate the returned value. -* [#16266](https://github.com/cosmos/cosmos-sdk/pull/16266) fix: legacy dec power mut zero exponent precision. +* [#16266](https://github.com/cosmos/cosmos-sdk/pull/16266) Fix legacy dec power mut zero exponent precision. ## [math/v1.0.1](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.1) - 2023-05-15 diff --git a/math/int.go b/math/int.go index e32b8ef3e0e8..cbbeb740f845 100644 --- a/math/int.go +++ b/math/int.go @@ -442,6 +442,10 @@ var ( ) func (i *Int) Size() (size int) { + if i == nil || i.i == nil { + return 1 + } + sign := i.Sign() if sign == 0 { // It is zero. // log*(0) is undefined hence return early. diff --git a/math/int_test.go b/math/int_test.go index e76cafc577d7..302e9ae2ee62 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -531,6 +531,7 @@ var sizeTests = []struct { s string want int }{ + {"", 1}, {"0", 1}, {"-0", 1}, {"-10", 3},