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

Unexcept nil pointer dereference with closure #804

Open
luoliwoshang opened this issue Sep 24, 2024 · 1 comment
Open

Unexcept nil pointer dereference with closure #804

luoliwoshang opened this issue Sep 24, 2024 · 1 comment

Comments

@luoliwoshang
Copy link
Contributor

luoliwoshang commented Sep 24, 2024

An unexpected invalid memory address or nil pointer dereference error occurs when performing a nil check (== nil) on a variable within the switch statement cases inside a closure.
This issue appears in all cases of the switch statement except for the first case.
The program successfully executes the first case but panics when attempting to execute any subsequent case.

package main

import (
	"fmt"
)

func getList(kind int) {
	params := []int{}
	visit(kind, func(childKind int) {
		switch childKind {
		case 1:
			fmt.Printf("Case 1:params is nil: %t\n", params == nil)
			fmt.Printf("Length of params: %d\n", len(params))
		case 2:
			fmt.Printf("Case 2:params is nil: %t\n", params == nil)
			fmt.Printf("Length of params: %d\n", len(params))
                case 3:
			fmt.Printf("Case 3:params is nil: %t\n", params == nil)
			fmt.Printf("Length of params: %d\n", len(params))
		}
	})
}

func visit(kind int, visitor func(int)) {
	visitor(kind)
}

func main() {
	getList(1)
	getList(2)
}

Expected

❯ go run .
Case 1:params is nil: false
Length of params: 0
Case 2:params is nil: false
Length of params: 0

Actual Got

❯ llgo run .
Case 1:params is nil: false
Length of params: 0
panic: runtime error: invalid memory address or nil pointer dereference

This behavior is consistent whether using a switch statement or an if-else structure. For example, the following if-else structure exhibits the same behavior:

if childKind == 1 {
    fmt.Printf("Case 1:params is nil: %t\n", params == nil)
    fmt.Printf("Length of params: %d\n", len(params))
} else if childKind == 2 {
    fmt.Printf("Case 2:params is nil: %t\n", params == nil)
    fmt.Printf("Length of params: %d\n", len(params))
} else if childKind == 3 {
    fmt.Printf("Case 3:params is nil: %t\n", params == nil)
    fmt.Printf("Length of params: %d\n", len(params))
}

In this case, the first if statement (childKind == 1) executes successfully, but the program panics when trying to execute either of the else if statements.

Env

llgo version: Latest main branch, commit dbaf12b

@luoliwoshang
Copy link
Contributor Author

#775 This PR can resolve this question

@luoliwoshang luoliwoshang changed the title unexcept nil pointer dereference with closure Unexcept nil pointer dereference with closure Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant