git bisect suggests that this behavior was introduced by the following commit:
commit 962ccbef91057f91518443b648e02fc3afe8c764 (HEAD) Author: Keith Randall <k...@golang.org> Date: Wed Oct 25 13:35:13 2023 -0700 cmd/compile: ensure pointer arithmetic happens after the nil check Have nil checks return a pointer that is known non-nil. Users of that pointer can use the result, ensuring that they are ordered after the nil check itself. The order dependence goes away after scheduling, when we've fixed an order. At that point we move uses back to the original pointer so it doesn't change regalloc any. This prevents pointer arithmetic on nil from being spilled to the stack and then observed by a stack scan. Fixes #63657 Change-Id: I1a5fa4f2e6d9000d672792b4f90dfc1b7b67f6ea Reviewed-on: https://go-review.googlesource.com/c/go/+/537775 Reviewed-by: David Chase <drch...@google.com> LUCI-TryBot-Result: Go LUCI <golang-sco...@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <k...@google.com> On Saturday, July 5, 2025 at 5:23:25 PM UTC+2 Ge wrote: > As follows showing, when `-N` is enabled with compiler, it gives the > expected output. > ``` > ➜ grd more nf2.go > package main > > import "os" > > func main() { > f, err := os.Open("nonExistFile") > fname := f.Name() > if err != nil { > println("failed to open file, Error:", err.Error()) > return > } > > println("filename:", fname) > } > ➜ grd go run nf2.go > failed to open file, Error: open nonExistFile: no such file or directory > > ➜ grd go run -gcflags="-N" nf2.go > panic: runtime error: invalid memory address or nil pointer dereference > [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4782e3] > > goroutine 1 [running]: > os.(*File).Name(...) > /usr/local/go/src/os/file.go:62 > main.main() > /root/grd/nf2.go:7 +0x123 > exit status 2 > > ➜ grd go version > go version go1.24.4 linux/amd64 > ``` > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/aa20237d-aa5a-45ee-901f-6a9c59c04ff1n%40googlegroups.com.