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

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

Open
alandonovan opened this Issue Oct 5, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@alandonovan
Contributor

alandonovan commented Oct 5, 2018

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

@dmitshur dmitshur changed the title from go list: crash when func Example has no body to cmd/go: go list crashes when func Example has no body Oct 6, 2018

@dmitshur dmitshur added this to the Go1.12 milestone Oct 6, 2018

@dmitshur

This comment has been minimized.

Member

dmitshur commented Oct 6, 2018

@mfrw

This comment has been minimized.

Contributor

mfrw commented Oct 6, 2018

Would it be a good idea if we skip the empty examples altogether?

@gopherbot

This comment has been minimized.

gopherbot commented Oct 6, 2018

Change https://golang.org/cl/140317 mentions this issue: go/doc: skip exmples with no body

@gopherbot gopherbot closed this in d30d5a6 Oct 9, 2018

@alandonovan alandonovan reopened this Nov 15, 2018

@alandonovan

This comment has been minimized.

Contributor

alandonovan commented Nov 15, 2018

A related crash is still unfixed:

$ cat c.go
package main

import (
        "fmt"
        "go/doc"
        "go/parser"
        "go/token"
        "log"
)

func main() {
        fset := token.NewFileSet()
        const src = `package p; func Example() {}; func Example_a()`
        f, err := parser.ParseFile(fset, "a.go", src, 0)
        if err != nil {
                log.Fatal(err)
        }

        fmt.Println(doc.Examples(f))
}

$ go run c.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4f8ba1]

goroutine 1 [running]:
go/ast.(*BlockStmt).Pos(...)
        /usr/local/google/home/adonovan/goroot/src/go/ast/ast.go:728
go/doc.lastComment(0x0, 0x0, 0x0, 0x0, 0x7f8e3f23d000, 0x20)
        /usr/local/google/home/adonovan/goroot/src/go/doc/example.go:424 +0x11
go/doc.stripOutputComment(0x0, 0x0, 0x0, 0x0, 0x1, 0xc0000102e0, 0x0, 0x1)
        /usr/local/google/home/adonovan/goroot/src/go/doc/example.go:405 +0x67
go/doc.playExampleFile(0xc0000b4000, 0x9)
        /usr/local/google/home/adonovan/goroot/src/go/doc/example.go:387 +0x4da
go/doc.Examples(0xc000055f60, 0x1, 0x1, 0x526b20, 0x57e9e0, 0x0)
        /usr/local/google/home/adonovan/goroot/src/go/doc/example.go:96 +0x286
main.main()
        /usr/local/google/home/adonovan/c.go:19 +0x104
exit status 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment