internal/lsp: check InRange before calling token.Offset

This shows up every now and then--maybe we need a wrapper function
around token.Offset to check the range.

Updates golang/go#48249

Change-Id: I9c60bc7cc61fcfb2f4e8c6963586d8b8fbb21835
Reviewed-on: https://go-review.googlesource.com/c/tools/+/348429
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Rebecca Stambler 2021-09-08 11:09:57 -04:00
parent e5f719fbe6
commit 0a6f080615
1 changed files with 3 additions and 1 deletions

View File

@ -341,7 +341,9 @@ func HoverInfo(ctx context.Context, s Snapshot, pkg Package, obj types.Object, p
tok2 := s.FileSet().File(node.Pos())
var spec ast.Spec
for _, s := range node.Specs {
if tok2.Offset(s.Pos()) <= offset && offset <= tok2.Offset(s.End()) {
// Avoid panics by guarding the calls to token.Offset (golang/go#48249).
// TODO(rstambler): Investigate this further and adjust if needed.
if InRange(tok2, s.Pos()) && InRange(tok2, s.End()) && tok2.Offset(s.Pos()) <= offset && offset <= tok2.Offset(s.End()) {
spec = s
break
}