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(vm): avoid "index out of range" in convertArgToGno #2500

Merged
merged 7 commits into from
Jul 9, 2024

Conversation

n0izn0iz
Copy link
Contributor

@n0izn0iz n0izn0iz commented Jul 4, 2024

With this gno code:

// foo.gno
func Foo(val uint64)

Currently, passing an empty string where a numeric argument is expected in a vm call such as:

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 ;)

@github-actions github-actions bot added the 📦 ⛰️ gno.land Issues or PRs gno.land package related label Jul 4, 2024
@n0izn0iz n0izn0iz changed the title fix(vm): avoid index-out-of-range in convertArgToGno fix(vm): avoid "index out of range" in convertArgToGno Jul 4, 2024
Copy link

codecov bot commented Jul 4, 2024

Codecov Report

Attention: Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.

Project coverage is 54.84%. Comparing base (f980fd6) to head (076ff0c).

Files Patch % Lines
gno.land/pkg/sdk/vm/convert.go 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2500      +/-   ##
==========================================
- Coverage   54.96%   54.84%   -0.12%     
==========================================
  Files         595      595              
  Lines       79568    79294     -274     
==========================================
- Hits        43734    43492     -242     
+ Misses      32527    32520       -7     
+ Partials     3307     3282      -25     
Flag Coverage Δ
contribs/gnodev 26.00% <ø> (ø)
contribs/gnofaucet 15.31% <ø> (+0.85%) ⬆️
contribs/gnokeykc 0.00% <ø> (ø)
contribs/gnomd 0.00% <ø> (ø)
gno.land 65.02% <86.66%> (+1.88%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thehowl thehowl merged commit 40a4e30 into gnolang:master Jul 9, 2024
80 checks passed
@omarsy omarsy deleted the convert-empty-num-better-err branch July 9, 2024 20:26
gfanton pushed a commit to gfanton/gno that referenced this pull request Jul 23, 2024
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 ⛰️ gno.land Issues or PRs gno.land package related
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants