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(gnolang): allow comparisons using uninitialized string values #1132

Merged
merged 7 commits into from
Oct 5, 2023

Conversation

thehowl
Copy link
Member

@thehowl thehowl commented Sep 16, 2023

The provided additional test, without the patch, fails with the following error:

=== RUN   TestFiles/comp3.gno
Machine.RunMain() panic: interface conversion: gnolang.Value is nil, not gnolang.StringValue
[...]
goroutine 180 [running]:
runtime/debug.Stack()
        /usr/lib/go/src/runtime/debug/stack.go:24 +0x65
[...]
panic({0xb91680, 0xc001fa7bf0})
        /usr/lib/go/src/runtime/panic.go:884 +0x213
github.com/gnolang/gno/gnovm/pkg/gnolang.isLss(0xc0054b4050, 0xc0054b4078)
        /home/howl/oc/gno2/gnovm/pkg/gnolang/op_binary.go:492 +0x431

This seems to be because internally, the string value is "uninitialized" (hopefully right word here). This is in opposition to an initialised string, as would happen for the statement x := "".

This PR changes the behaviour for comparisons inside of op_binary (<, >, <=, >=) to use GetString instead of a type assertion of TypedValue.V to a StringValue. This is in line with what is already done inside of isEql, introduced in this commit:

da6520f#diff-7cc5c6bc5496b6ad9ed55e04e1cdf2f0d1d5954af21be5bc38ef3c46389149a9L358

git blame points instead this part of code inside of the comparisons functions to go back to the initial commit.

Additionally, I looked for other cases where we are currently doing type assertions directly into a StringValue, and there was one in the implementation of append. Since this is a special case and requires having a native value as the first argument, I haven't written a test for it, but the change should be safe as GetString() internally just does the type assertion, but checks for tv.V == nil first.

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • 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, if any. More info here.

@thehowl thehowl added 🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related labels Sep 16, 2023
@thehowl thehowl added this to the 🌟 main.gno.land (wanted) milestone Sep 16, 2023
@thehowl thehowl self-assigned this Sep 16, 2023
@thehowl thehowl requested review from jaekwon, moul and a team as code owners September 16, 2023 12:58
@codecov
Copy link

codecov bot commented Sep 18, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (ce258b1) 47.31% compared to head (f52130f) 47.33%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1132      +/-   ##
==========================================
+ Coverage   47.31%   47.33%   +0.01%     
==========================================
  Files         367      367              
  Lines       62118    62118              
==========================================
+ Hits        29394    29402       +8     
  Misses      30325    30325              
+ Partials     2399     2391       -8     
Files Coverage Δ
gnovm/pkg/gnolang/uverse.go 19.58% <0.00%> (ø)
gnovm/pkg/gnolang/op_binary.go 4.79% <0.00%> (ø)

... and 7 files with indirect coverage changes

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

@thehowl
Copy link
Member Author

thehowl commented Oct 5, 2023

Codecov will mark this as not tested without #1122 . I'd say this is still good to merge, though. (Moved to review meeting col)

@thehowl thehowl merged commit 4af3eb2 into master Oct 5, 2023
182 checks passed
@thehowl thehowl deleted the dev/morgan/compare-zero-val branch October 5, 2023 17:32
gfanton pushed a commit to gfanton/gno that referenced this pull request Nov 9, 2023
…olang#1132)

The provided additional test, without the patch, fails with the
following error:

```
=== RUN   TestFiles/comp3.gno
Machine.RunMain() panic: interface conversion: gnolang.Value is nil, not gnolang.StringValue
[...]
goroutine 180 [running]:
runtime/debug.Stack()
        /usr/lib/go/src/runtime/debug/stack.go:24 +0x65
[...]
panic({0xb91680, 0xc001fa7bf0})
        /usr/lib/go/src/runtime/panic.go:884 +0x213
github.com/gnolang/gno/gnovm/pkg/gnolang.isLss(0xc0054b4050, 0xc0054b4078)
        /home/howl/oc/gno2/gnovm/pkg/gnolang/op_binary.go:492 +0x431
```

This seems to be because internally, the string value is "uninitialized"
(hopefully right word here). This is in opposition to an initialised
string, as would happen for the statement `x := ""`.

This PR changes the behaviour for comparisons inside of `op_binary` (<,
>, <=, >=) to use `GetString` instead of a type assertion of
`TypedValue.V` to a `StringValue`. This is in line with what is already
done inside of `isEql`, introduced in this commit:


gnolang@da6520f#diff-7cc5c6bc5496b6ad9ed55e04e1cdf2f0d1d5954af21be5bc38ef3c46389149a9L358

`git blame` points instead this part of code inside of the comparisons
functions to go back to the initial commit.

Additionally, I looked for other cases where we are currently doing type
assertions directly into a `StringValue`, and there was one in the
implementation of `append`. Since this is a special case and requires
having a native value as the first argument, I haven't written a test
for it, but the change should be safe as `GetString()` internally just
does the type assertion, but checks for `tv.V == nil` first.

<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 or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] 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>

---------

Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: 🚀 Needed for Launch
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants