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

Inconsistent Behavior with len on Nil Array Pointer in tinygo #4786

Open
Yeaseen opened this issue Mar 7, 2025 · 5 comments
Open

Inconsistent Behavior with len on Nil Array Pointer in tinygo #4786

Yeaseen opened this issue Mar 7, 2025 · 5 comments

Comments

@Yeaseen
Copy link

Yeaseen commented Mar 7, 2025

The len function behaves inconsistently when applied to a nil array pointer in TinyGo compared to standard Go. While Go 1.24.1 returns the array length without a panic, TinyGo produces a runtime error when dereferencing a nil array pointer.

$ cat test.go
package main
func nilPtrToArray() *[2]byte {
        println("Some Activity")
        return nil
}
func main() {
        println(len(*nilPtrToArray()))

$ go version
go version go1.24.1 linux/amd64

$ go run test.go
Some Activity
2

$ tinygo run test.go
Some Activity
panic: runtime error at 0x0000000000203697: nil pointer dereference
[tinygo: panic at test.go:7:14]
failed to run compiled binary /tmp/tinygo782597653/main: signal: aborted

$ tinygo version
tinygo version 0.36.0 linux/amd64 (using go version go1.24.1 and LLVM version 19.1.2)

Expected Behavior

In Go, len(*nilPtrToArray()) should not result in a panic. Instead, it should return the length of the array type (in this case, 2), as the len function in Go does not evaluate the contents of the array but rather its static length. tinygo should replicate this behavior to maintain compatibility.

Analysis

The discrepancy likely stems from how tinygo handles nil pointer dereferencing in combination with len. In standard Go, dereferencing a nil pointer to an array solely for evaluating its length does not trigger a runtime error. tinygo, however, appears to strictly enforce pointer validity, leading to a nil pointer dereference panic.

@Yeaseen
Copy link
Author

Yeaseen commented Mar 7, 2025

I think cap function also shows the same discrepancy.

func main() {
	println(cap(*nilPtrToArray()))
}

@aykevl
Copy link
Member

aykevl commented Mar 13, 2025

I can confirm this issue.

@aykevl
Copy link
Member

aykevl commented Mar 13, 2025

Looks like this is actually a bug in the golang.org/x/tools/go/ssa package. I'll see whether I can create a reproducer for the Go team.

@aykevl
Copy link
Member

aykevl commented Mar 13, 2025

Reported the issue upstream: golang/go#72844

@Yeaseen
Copy link
Author

Yeaseen commented Mar 13, 2025

@aykevl It looks like a Go compiler bug! Thank you for submitting the issue on upstream.

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

2 participants