Skip to content

Commit

Permalink
fix(sdk/vm): improve MsgCall panic error message for wrong number of …
Browse files Browse the repository at this point in the history
…args (gnolang#1610)

If a realm function Call has too many arguments, VMKeeper Call panics
with an uninformative index error message like "index out of range [1]
with length 1" when it is [checking the
arguments](https://github.com/gnolang/gno/blob/12b4b458e1b13d491a5797aa11b2002242f012bd/gno.land/pkg/sdk/vm/keeper.go#L244).
One of our devs lost time trying to figure this out, as would other devs
presumably.

This PR adds explicit checks for number of arguments with informative
panic messages like "not enough arguments in call to Echo" or "too many
arguments in call to Echo" . (These are the same errors as the Go
compiler.)

(I couldn't find an existing issue for this. If you already have other
plans to improve this error message, then you can close this PR.)

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Signed-off-by: Jeff Thompson <[email protected]>
Co-authored-by: Morgan <[email protected]>
  • Loading branch information
2 people authored and leohhhn committed Feb 8, 2024
1 parent e74cac5 commit 6a090a1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
if cx.Varg {
panic("variadic calls not yet supported")
}
if len(msg.Args) != len(ft.Params) {
panic(fmt.Sprintf("wrong number of arguments in call to %s: want %d got %d", fnc, len(ft.Params), len(msg.Args)))
}
for i, arg := range msg.Args {
argType := ft.Params[i].Type
atv := convertArgToGno(arg, argType)
Expand Down
41 changes: 41 additions & 0 deletions gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,44 @@ func main() {
expectedString := fmt.Sprintf("hello world! %s\n", addr.String())
assert.Equal(t, res, expectedString)
}

func TestNumberOfArgsError(t *testing.T) {
env := setupTestEnv()
ctx := env.ctx

// Give "addr1" some gnots.
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins("10000000ugnot"))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins("10000000ugnot")))

// Create test package.
files := []*std.MemFile{
{
Name: "test.gno",
Body: `package test
import "std"
func Echo(msg string) string {
return "echo:"+msg
}`,
},
}
pkgPath := "gno.land/r/test"
msg1 := NewMsgAddPackage(addr, pkgPath, files)
err := env.vmk.AddPackage(ctx, msg1)
assert.NoError(t, err)

// Call Echo function with wrong number of arguments
coins := std.MustParseCoins("1ugnot")
msg2 := NewMsgCall(addr, coins, pkgPath, "Echo", []string{"hello world", "extra arg"})
assert.PanicsWithValue(
t,
func() {
env.vmk.Call(ctx, msg2)
},
"wrong number of arguments in call to Echo: want 1 got 2",
)
}

0 comments on commit 6a090a1

Please sign in to comment.