Skip to content

Commit 8b6e84b

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/crash: don't crash in xrefs on out of bound nodes
As we've seen many times, it is currently (unfortunately) possible that go/ast nodes exceed the bounds of the parsed file. Handle this possibility correctly while building the xrefs index. Updates golang/go#66683 Fixes golang/go#70446 Change-Id: If6364876eb7b8ed8ca11a058417aa028d6b55b41 Reviewed-on: https://go-review.googlesource.com/c/tools/+/630675 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 936a401 commit 8b6e84b

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

gopls/internal/cache/xrefs/xrefs.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.org/x/tools/gopls/internal/cache/metadata"
1818
"golang.org/x/tools/gopls/internal/cache/parsego"
1919
"golang.org/x/tools/gopls/internal/protocol"
20+
"golang.org/x/tools/gopls/internal/util/bug"
2021
"golang.org/x/tools/gopls/internal/util/frob"
2122
)
2223

@@ -43,15 +44,6 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte {
4344
objectpathFor := new(objectpath.Encoder).For
4445

4546
for fileIndex, pgf := range files {
46-
47-
nodeRange := func(n ast.Node) protocol.Range {
48-
rng, err := pgf.PosRange(n.Pos(), n.End())
49-
if err != nil {
50-
panic(err) // can't fail
51-
}
52-
return rng
53-
}
54-
5547
ast.Inspect(pgf.File, func(n ast.Node) bool {
5648
switch n := n.(type) {
5749
case *ast.Ident:
@@ -82,10 +74,15 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte {
8274
objects[obj] = gobObj
8375
}
8476

85-
gobObj.Refs = append(gobObj.Refs, gobRef{
86-
FileIndex: fileIndex,
87-
Range: nodeRange(n),
88-
})
77+
// golang/go#66683: nodes can under/overflow the file.
78+
// For example, "var _ = x." creates a SelectorExpr(Sel=Ident("_"))
79+
// that is beyond EOF. (Arguably Ident.Name should be "".)
80+
if rng, err := pgf.NodeRange(n); err == nil {
81+
gobObj.Refs = append(gobObj.Refs, gobRef{
82+
FileIndex: fileIndex,
83+
Range: rng,
84+
})
85+
}
8986
}
9087
}
9188

@@ -102,10 +99,15 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte {
10299
gobObj = &gobObject{Path: ""}
103100
objects[nil] = gobObj
104101
}
105-
gobObj.Refs = append(gobObj.Refs, gobRef{
106-
FileIndex: fileIndex,
107-
Range: nodeRange(n.Path),
108-
})
102+
// golang/go#66683: nodes can under/overflow the file.
103+
if rng, err := pgf.NodeRange(n.Path); err == nil {
104+
gobObj.Refs = append(gobObj.Refs, gobRef{
105+
FileIndex: fileIndex,
106+
Range: rng,
107+
})
108+
} else {
109+
bug.Reportf("out of bounds import spec %+v", n.Path)
110+
}
109111
}
110112
return true
111113
})

0 commit comments

Comments
 (0)