Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: x/gov/client/cli: fix integer overflow in parsing int prompted values #13347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion x/gov/client/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ func Prompt[T any](data T, namePrefix string) (T, error) {
case reflect.String:
v.Field(i).SetString(result)
case reflect.Int:
resultInt, _ := strconv.Atoi(result)
resultInt, err := strconv.Atoi(result)
if err != nil {
return data, fmt.Errorf("invalid value for int: %w", err)
}
// If a value was successfully parsed the ranges of:
// [minInt, maxInt]
// are within the ranges of:
// [minInt64, maxInt64]
// of which on 64-bit machines, which are most common,
// int==int64
v.Field(i).SetInt(int64(resultInt))
default:
// skip other types
Expand Down
43 changes: 43 additions & 0 deletions x/gov/client/cli/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cli_test

import (
"sync"
"testing"

"github.com/chzyer/readline"
"github.com/stretchr/testify/assert"

"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
)

type st struct {
ToOverflow int
}

// On the tests running in Github actions, somehow there are races.
var globalMu sync.Mutex
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

// Tests that we successfully report overflows in parsing ints
// See https://github.com/cosmos/cosmos-sdk/issues/13346
func TestPromptIntegerOverflow(t *testing.T) {
// Intentionally sending a value out of the range of
intOverflowers := []string{
"-9223372036854775809",
"9223372036854775808",
"9923372036854775809",
"-9923372036854775809",
"18446744073709551616",
"-18446744073709551616",
}

for _, intOverflower := range intOverflowers {
overflowStr := intOverflower
t.Run(overflowStr, func(t *testing.T) {
readline.Stdout.Write([]byte(overflowStr + "\n"))

v, err := cli.Prompt(st{}, "")
assert.NotNil(t, err, "expected a report of an overflow")
assert.Equal(t, st{}, v, "expected a value of zero")
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
})
}
}