This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: x/gov/client/cli: fix integer overflow in parsing int prompted v…
…alues (cosmos#13347) Reported by cosmos/gosec in https://github.com/cosmos/cosmos-sdk/security/code-scanning/5730. This change checks for errors from strconv.Atoi in which case we were susceptible to out of range errors, this change also adds tests to prevent regressions. Fixes cosmos#13346
- Loading branch information
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
// 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") | ||
}) | ||
} | ||
} |