forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vm): avoid "index out of range" in
convertArgToGno
(gnolang#2500)
With this gno code: ```go // foo.gno func Foo(val uint64) ``` Currently, passing an empty string where a numeric argument is expected in a vm call such as: ```go msg := MsgCall{ // (...) Func: "Foo", Args: []string{""}, } vmkeeper.Call(ctx, msg) ``` Will error out with: ``` runtime error: index out of range [0] with length 0 ``` Because the argument conversion routine accesses the first character to see if it's a `+` without first checking the length of the string This runtime error is confusing IMO and this PR makes the resulting error more meaningful, for example: ``` error parsing uint64 "": strconv.ParseUint: parsing "": invalid syntax ``` also increases `gnoland` code coverage by ~2% as a bonus ;) --------- Signed-off-by: Norman Meier <[email protected]>
- Loading branch information
Showing
2 changed files
with
55 additions
and
15 deletions.
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,39 @@ | ||
package vm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/gnolang/gno/gnovm/pkg/gnolang" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertEmptyNumbers(t *testing.T) { | ||
tests := []struct { | ||
argT gnolang.Type | ||
expectedErr string | ||
}{ | ||
{gnolang.UintType, `error parsing uint "": strconv.ParseUint: parsing "": invalid syntax`}, | ||
{gnolang.Uint64Type, `error parsing uint64 "": strconv.ParseUint: parsing "": invalid syntax`}, | ||
{gnolang.Uint32Type, `error parsing uint32 "": strconv.ParseUint: parsing "": invalid syntax`}, | ||
{gnolang.Uint16Type, `error parsing uint16 "": strconv.ParseUint: parsing "": invalid syntax`}, | ||
{gnolang.Uint8Type, `error parsing uint8 "": strconv.ParseUint: parsing "": invalid syntax`}, | ||
{gnolang.IntType, `error parsing int "": strconv.ParseInt: parsing "": invalid syntax`}, | ||
{gnolang.Int64Type, `error parsing int64 "": strconv.ParseInt: parsing "": invalid syntax`}, | ||
{gnolang.Int32Type, `error parsing int32 "": strconv.ParseInt: parsing "": invalid syntax`}, | ||
{gnolang.Int16Type, `error parsing int16 "": strconv.ParseInt: parsing "": invalid syntax`}, | ||
{gnolang.Int8Type, `error parsing int8 "": strconv.ParseInt: parsing "": invalid syntax`}, | ||
{gnolang.Float64Type, `error parsing float64 "": parse mantissa: `}, | ||
{gnolang.Float32Type, `error parsing float32 "": parse mantissa: `}, | ||
} | ||
|
||
for _, tt := range tests { | ||
testname := fmt.Sprintf("%v", tt.argT) | ||
t.Run(testname, func(t *testing.T) { | ||
run := func() { | ||
_ = convertArgToGno("", tt.argT) | ||
} | ||
assert.PanicsWithValue(t, tt.expectedErr, run) | ||
}) | ||
} | ||
} |