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

TinyGo Panics on Nil Pointer Dereference in Map Lookup, Unlike Standard Go #4816

Closed
Yeaseen opened this issue Mar 18, 2025 · 4 comments
Closed

Comments

@Yeaseen
Copy link

Yeaseen commented Mar 18, 2025

TinyGo incorrectly panics on a nil pointer dereference when accessing a struct field from a map lookup, while standard Go handles the same code without errors. This inconsistency suggests that TinyGo does not optimize nil pointer access in maps the same way as the Go compiler does.

$ cat test.go
package main
import "fmt"
type thing struct {
        field int
}
func main() {
        m := map[string]*thing{}
        t, ok := m["abc"]
        valid := t.field >= 0

        if !ok || !valid {
                fmt.Printf("got: %v\n", t)
                t = &thing{field: 0}
                m["abc"] = t
        }

        t.field += 3
        fmt.Println(t.field)
}
$ go version
go version go1.24.1 linux/amd64

$ go run test.go
got: <nil>
3
$ tinygo version
tinygo version 0.36.0 linux/amd64 (using go version go1.24.1 and LLVM version 19.1.2)

$ tinygo run test.go
panic: runtime error at 0x000000000021261d: nil pointer dereference
[tinygo: panic at /usr/local/lib/tinygo/src/runtime/scheduler_cooperative.go]
failed to run compiled binary /tmp/tinygo4289749700/main: signal: aborted
@aykevl
Copy link
Member

aykevl commented Mar 18, 2025

Actually the panic looks correct to me: t is nil so t.field is a nil pointer dereference.
Might be another bug in the Go compiler?

@aykevl
Copy link
Member

aykevl commented Mar 18, 2025

Here you go: golang/go#72918
Pretty sure this is actually a bug in the Go compiler.

...how do you find these edge cases? This is the second one in a short time.

@aykevl
Copy link
Member

aykevl commented Mar 18, 2025

Ok, this is indeed a bug in the Go compiler. Not a TinyGo bug.

The reproducer is very similar to golang/go#72860. I guess it originated from the same source?

@aykevl aykevl closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2025
@Yeaseen
Copy link
Author

Yeaseen commented Mar 19, 2025

@aykevl Yeah. I believe so. I just found the discrepancy in the outputs between tinygo and go.

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