Skip to content

fix: nil ptr when discarded key and value not present on range#5751

Merged
ltzmaxwell merged 10 commits into
gnolang:masterfrom
Villaquiranm:fix-5664
Jun 10, 2026
Merged

fix: nil ptr when discarded key and value not present on range#5751
ltzmaxwell merged 10 commits into
gnolang:masterfrom
Villaquiranm:fix-5664

Conversation

@Villaquiranm

Copy link
Copy Markdown
Contributor

fixes #5664
The range of the form

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

The fix is to add this case to the early return

…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)
@github-actions github-actions Bot added the 📦 🤖 gnovm Issues or PRs gnovm related label May 29, 2026
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label May 29, 2026
@Gno2D2

Gno2D2 commented May 29, 2026

Copy link
Copy Markdown
Collaborator

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
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)
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: Villaquiranm/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🟢 User davd-gzl already reviewed PR 5751 with state APPROVED
    │       ├── 🟢 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🟢 Then
        └── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

@codecov

codecov Bot commented May 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@davd-gzl davd-gzl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Villaquiranm

Villaquiranm commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

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 👍

Comment thread gnovm/cmd/gno/testdata/test/issue_5664.txtar Outdated
@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jun 2, 2026

@ltzmaxwell ltzmaxwell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread gnovm/cmd/gno/testdata/test/issue_5664.txtar Outdated
@ltzmaxwell ltzmaxwell merged commit 16b40ab into gnolang:master Jun 10, 2026
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📦 🤖 gnovm Issues or PRs gnovm related

Projects

Development

Successfully merging this pull request may close these issues.

Gno panics on fixedbugs/bug406.go (Go runs it without error)

4 participants