Skip to content

Commit

Permalink
backport #13350
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Sep 22, 2022
1 parent f51df8b commit 3c757ab
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
13 changes: 11 additions & 2 deletions x/gov/client/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,17 @@ 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)
v.Field(i).SetInt(int64(resultInt))
resultInt, err := strconv.ParseInt(result, 10, 0)
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(resultInt)
default:
// skip other types
// possibly in the future we can add more types (like slices)
Expand Down
88 changes: 88 additions & 0 deletions x/gov/client/cli/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//go:build !race
// +build !race

// Disabled -race because the package github.com/manifoldco/promptui@v0.9.0
// has a data race and this code exposes it, but fixing it would require
// holding up the associated change to this.

package cli_test

import (
"fmt"
"math"
"os"
"testing"

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

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

type st struct {
I int
}

// 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 values out of the range of int.
intOverflowers := []string{
"-9223372036854775809",
"9223372036854775808",
"9923372036854775809",
"-9923372036854775809",
"18446744073709551616",
"-18446744073709551616",
}

for _, intOverflower := range intOverflowers {
overflowStr := intOverflower
t.Run(overflowStr, func(t *testing.T) {
origStdin := readline.Stdin
defer func() {
readline.Stdin = origStdin
}()

fin, fw := readline.NewFillableStdin(os.Stdin)
readline.Stdin = fin
fw.Write([]byte(overflowStr + "\n"))

v, err := cli.Prompt(st{}, "")
assert.Equal(t, st{}, v, "expected a value of zero")
require.NotNil(t, err, "expected a report of an overflow")
require.Contains(t, err.Error(), "range")
})
}
}

func TestPromptParseInteger(t *testing.T) {
// Intentionally sending a value out of the range of
values := []struct {
in string
want int
}{
{fmt.Sprintf("%d", math.MinInt), math.MinInt},
{"19991", 19991},
{"991000000199", 991000000199},
}

for _, tc := range values {
tc := tc
t.Run(tc.in, func(t *testing.T) {
origStdin := readline.Stdin
defer func() {
readline.Stdin = origStdin
}()

fin, fw := readline.NewFillableStdin(os.Stdin)
readline.Stdin = fin
fw.Write([]byte(tc.in + "\n"))

v, err := cli.Prompt(st{}, "")
assert.Nil(t, err, "expected a nil error")
assert.Equal(t, tc.want, v.I, "expected %d = %d", tc.want, v.I)
})
}
}

0 comments on commit 3c757ab

Please sign in to comment.