fix: nil ptr when discarded key and value not present on range#5751
Conversation
…resent on range the txtar specific case: for _ = range a.e should cause an early return on (x *RangeStmt) AssertCompatible, as both values key and value of the range are discarded. The problem is when the range is on the form `for _ := range` . the key is considered blankIdentifier but the value is not present (nil)
🛠 PR Checks SummaryAll Automated Checks passed. ✅ Manual Checks (for Reviewers):
Read More🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers. ✅ Automated Checks (for Contributors):🟢 Maintainers must be able to edit this pull request (more info) ☑️ Contributor Actions:
☑️ Reviewer Actions:
📚 Resources:Debug
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Try:
# from a local clone of gnolang/gno:
gh pr checkout 5751 -R gnolang/gno
cat > gnovm/tests/files/range_blank_key_5751.gno <<'EOF'
package main
func main() {
a := []int{1, 2, 3}
var v int
for _, v = range a {
}
_ = v
println("ok")
}
// Output:
// ok
EOF
go test -run 'TestFiles/range_blank_key_5751.gno$' ./gnovm/pkg/gnolang/
rm gnovm/tests/files/range_blank_key_5751.gno
Output:
--- FAIL: TestFiles/range_blank_key_5751.gno (0.00s)
files_test.go:111: unexpected panic: runtime error: invalid memory address or nil pointer dereference
...
gnolang.assertIndexTypeIsInt({0x0, 0x0}) type_check.go:823
gnolang.(*RangeStmt).AssertCompatible(...) type_check.go:853
Thanks for the review and the test, this is indeed a problem :) But in my opinion is not inside the scope of the issue this PR was trying to address (even if it is closely related). I was going to check and implement a fix for this but I saw this PR #5764 from @ltzmaxwell that already fix it I verified with your test 👍 |
There was a problem hiding this comment.
i will lean to a filetest for this kind a single file test for VM:
package main
type matrix struct {
e []int
}
func (a matrix) equal() bool {
for _ = range a.e {
}
for _, _ = range a.e {
}
for range a.e {
}
return true
}
func main() {
var a matrix
var i interface{}
i = true && a.equal()
println(i)
}
// Output:
// true
fixes #5664
The range of the form
Should cause an early return on (x *RangeStmt) AssertCompatible, as both values key and value of the range are discarded. The problem is when the range is on the form
for _ := range. the key is considered blankIdentifier but the value is not present (nil).The fix is to add this case to the early return