1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:44:43 -07:00

cmd/guru: fix nil deref when position is not in Go source

+ test

Fixes issue golang/go#14684

Change-Id: I56023bf36c307d02c71b4ddf08aee9c229fc66c1
Reviewed-on: https://go-review.googlesource.com/20247
Reviewed-by: Daniel Morsing <daniel.morsing@gmail.com>
This commit is contained in:
Alan Donovan 2016-03-07 09:47:24 -05:00
parent b15543bacc
commit d2abdd5413
3 changed files with 22 additions and 0 deletions

View File

@ -272,3 +272,20 @@ func TestGuru(t *testing.T) {
}
}
}
func TestIssue14684(t *testing.T) {
var buildContext = build.Default
buildContext.GOPATH = "testdata"
query := guru.Query{
Mode: "freevars",
Pos: "testdata/src/README.txt:#1",
Build: &buildContext,
}
err := guru.Run(&query)
if err == nil {
t.Fatal("guru query succeeded unexpectedly")
}
if got, want := err.Error(), "testdata/src/README.txt is not a Go source file"; got != want {
t.Errorf("query error was %q, want %q", got, want)
}
}

View File

@ -124,6 +124,9 @@ func fastQueryPos(ctxt *build.Context, pos string) (*queryPos, error) {
if f == nil {
return nil, err
}
if !f.Pos().IsValid() {
return nil, fmt.Errorf("%s is not a Go source file", filename)
}
start, end, err := fileOffsetToPos(fset.File(f.Pos()), startOffset, endOffset)
if err != nil {

2
cmd/guru/testdata/src/README.txt vendored Normal file
View File

@ -0,0 +1,2 @@
This is not a Go source file.
Used by TestIssue14684.