Skip to content

cmd/go: go list crashes when func Example has no body #28044

@alandonovan

Description

@alandonovan

The underlying cause appears to be an unguarded nil dereference of ast.File.Body in go/doc.Examples.

$ cd $GOPATH/src
$ cat test/example_test.go
package test_test

func Example()

$ go version
go version devel +497fd2c09c Fri Oct 5 17:56:34 2018 +0000 linux/amd64
$ go list -test test
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x55bdb1]

goroutine 1 [running]:
go/ast.(*BlockStmt).Pos(...)
	/home/adonovan/go/src/go/ast/ast.go:728
go/doc.lastComment(0x0, 0x0, 0x0, 0x0, 0x558f94, 0xc000105444)
	/home/adonovan/go/src/go/doc/example.go:409 +0x11
go/doc.exampleOutput(0x0, 0x0, 0x0, 0x0, 0x1, 0xc000344cb8, 0x4f236d)
	/home/adonovan/go/src/go/doc/example.go:108 +0x4d
go/doc.Examples(0xc000344e38, 0x1, 0x1, 0x9, 0x0, 0x4)
	/home/adonovan/go/src/go/doc/example.go:75 +0x559
cmd/go/internal/load.(*testFuncs).load(0xc0000ba930, 0xc0004cc0c0, 0x3f, 0x976c2f, 0x6, 0xc0000ba98a, 0xc0000ba98b, 0xc000121010, 0x5c23bb)
	/home/adonovan/go/src/cmd/go/internal/load/test.go:530 +0x107
cmd/go/internal/load.loadTestFuncs(0xc0000ab680, 0x7, 0x0, 0x0)
	/home/adonovan/go/src/cmd/go/internal/load/test.go:417 +0x257
cmd/go/internal/load.TestPackagesFor(0xc0000ab680, 0x0, 0x1, 0xc00000e640, 0x1, 0x1, 0x21)
	/home/adonovan/go/src/cmd/go/internal/load/test.go:234 +0x18dd
cmd/go/internal/list.runList(0xd76880, 0xc00001e0f0, 0x1, 0x1)
	/home/adonovan/go/src/cmd/go/internal/list/list.go:449 +0x19a7
main.main()
	/home/adonovan/go/src/cmd/go/main.go:219 +0x843

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeGoCommandcmd/goNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions