From 43cfdfe61894f0fde1e0a967cc0a85a5b2350eee Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Thu, 6 Apr 2023 11:10:30 -0300 Subject: [PATCH] fix(math): FormatInt returns error on empty string (#15714) --- math/CHANGELOG.md | 6 ++++++ math/int.go | 4 ++++ math/int_test.go | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 9ebf9077ae..e6d8dbc285 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -34,6 +34,12 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j # Changelog +## [Unreleased] + +### Bug Fixes + +* [#15714](https://github.com/cosmos/cosmos-sdk/pull/15714) `FormatInt` returns an error on empty string + ## [math/v1.0.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.0) - 2023-03-23 ### Bug Fixes diff --git a/math/int.go b/math/int.go index 525608e9a9..3e6b5c257d 100644 --- a/math/int.go +++ b/math/int.go @@ -457,6 +457,10 @@ var stringsBuilderPool = &sync.Pool{ // string following ADR-050. This function operates with string manipulation // (instead of manipulating the int or sdk.Int object). func FormatInt(v string) (string, error) { + if len(v) == 0 { + return "", fmt.Errorf("cannot format empty string") + } + sign := "" if v[0] == '-' { sign = "-" diff --git a/math/int_test.go b/math/int_test.go index c86165c712..56b05cc69f 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -473,6 +473,11 @@ func TestFormatIntNonDigits(t *testing.T) { } } +func TestFormatIntEmptyString(t *testing.T) { + _, err := math.FormatInt("") + require.ErrorContains(t, err, "cannot format empty string") +} + func TestFormatIntCorrectness(t *testing.T) { tests := []struct { in string