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

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

Closed
alandonovan opened this issue Oct 5, 2018 · 5 comments
Closed

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

alandonovan opened this issue Oct 5, 2018 · 5 comments
Labels
FrozenDueToAge GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@alandonovan
Copy link
Contributor

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 go list: crash when func Example has no body cmd/go: go list crashes when func Example has no body Oct 6, 2018
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. GoCommand cmd/go labels Oct 6, 2018
@dmitshur dmitshur added this to the Go1.12 milestone Oct 6, 2018
@dmitshur
Copy link
Contributor

dmitshur commented Oct 6, 2018

/cc @bcmills @rsc @ianlancetaylor

@mfrw
Copy link
Contributor

mfrw commented Oct 6, 2018

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

@gopherbot
Copy link

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

@alandonovan
Copy link
Contributor Author

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

@bcmills
Copy link
Contributor

bcmills commented Dec 14, 2018

That one looks like a bug in go/doc rather than cmd/go. I'll file it separately.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants