go version go1.10 linux/amd64
The following snippet causes the stacktrace to point to the wrong source code line.
package main
import (
"fmt"
"runtime/debug"
"github.com/pkg/errors"
)
type Bar struct {
i int
}
type Foo struct {
b *Bar
}
func main() {
defer func() {
if rvr := recover(); rvr != nil {
fmt.Printf("pkg/errors stacktrace:\n %+v\n\n", errors.Errorf("panic: %+v", rvr))
fmt.Printf("golang stacktrace:\n %s\n", debug.Stack())
}
}()
slice := []*Foo{
&Foo{},
nil,
}
for i, f := range slice {
fmt.Println("index", i)
if f.b == nil {
f.b = &Bar{}
}
fmt.Println(i, f.b.i)
}
}
A nil pointer deref occurs on line if f.b == nil {.
The pkg/errors stacktrace points to the wrong line: for i, f := range slice { (2 lines above).
The golang stacktrace points to the correct line.
pkg/errors stacktrace:
panic: runtime error: invalid memory address or nil pointer dereference
main.main.func1
xxx/main.go:21
runtime.call32
/usr/lib/go/src/runtime/asm_amd64.s:573
runtime.gopanic
/usr/lib/go/src/runtime/panic.go:505
runtime.panicmem
/usr/lib/go/src/runtime/panic.go:63
runtime.sigpanic
/usr/lib/go/src/runtime/signal_unix.go:388
main.main
xxx/main.go:31
runtime.main
/usr/lib/go/src/runtime/proc.go:198
runtime.goexit
/usr/lib/go/src/runtime/asm_amd64.s:2361
golang stacktrace:
goroutine 1 [running]:
runtime/debug.Stack(0x4bba7f, 0x1d, 0xc420059d58)
/usr/lib/go/src/runtime/debug/stack.go:24 +0xa7
main.main.func1()
xxx/main.go:22 +0x105
panic(0x49f8c0, 0x52a380)
/usr/lib/go/src/runtime/panic.go:505 +0x229
main.main()
xxx/main.go:33 +0x1c8
go version go1.10 linux/amd64
The following snippet causes the stacktrace to point to the wrong source code line.
A nil pointer deref occurs on line
if f.b == nil {.The pkg/errors stacktrace points to the wrong line:
for i, f := range slice {(2 lines above).The golang stacktrace points to the correct line.