From 0a6f0806153c54cb73e150dd60ec0cf6d6091290 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 8 Sep 2021 11:09:57 -0400 Subject: [PATCH] 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 Run-TryBot: Rebecca Stambler Reviewed-by: Robert Findley gopls-CI: kokoro TryBot-Result: Go Bot --- internal/lsp/source/hover.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index 10fb5417ae..f7fb3cb690 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -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 }