fix(math): FormatInt returns error on empty string (#15714)

This commit is contained in:
Facundo Medica 2023-04-06 11:10:30 -03:00 committed by GitHub
parent 17debf2efa
commit 43cfdfe618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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 = "-"

View File

@ -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