diff options
author | Rebecca Stambler <rstambler@golang.org> | 2021-10-04 15:14:38 -0400 |
---|---|---|
committer | Rebecca Stambler <rstambler@golang.org> | 2021-10-04 19:52:26 +0000 |
commit | db89b5a197b56cba19e5b0edb49eb3912aebbcf7 (patch) | |
tree | 1962b805e2329de8a706a25ae859b8477cdbddac /internal/lsp/cache/parse.go | |
parent | ccaa9074797e84c7beacf3cf406a143e2ec0a578 (diff) | |
download | golang-x-tools-db89b5a197b56cba19e5b0edb49eb3912aebbcf7.tar.gz |
internal/lsp: handle nil pointer in fixInitStmt
I'm going to make a follow-up CL to create a source.Offset function
that will always check inRange, and we should use that everywhere
instead of token.Offset, which is pretty prone to panicking.
Fixes golang/go#48763
Change-Id: Ia81309400d15a28c133f4b3d41c6239231c2532d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/353889
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>
Diffstat (limited to 'internal/lsp/cache/parse.go')
-rw-r--r-- | internal/lsp/cache/parse.go | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/internal/lsp/cache/parse.go b/internal/lsp/cache/parse.go index 742f48f6a..fc110c710 100644 --- a/internal/lsp/cache/parse.go +++ b/internal/lsp/cache/parse.go @@ -995,6 +995,10 @@ func fixInitStmt(bad *ast.BadExpr, parent ast.Node, tok *token.File, src []byte) } // Try to extract a statement from the BadExpr. + // Make sure that the positions are in range first. + if !source.InRange(tok, bad.Pos()) || !source.InRange(tok, bad.End()-1) { + return + } stmtBytes := src[tok.Offset(bad.Pos()) : tok.Offset(bad.End()-1)+1] stmt, err := parseStmt(bad.Pos(), stmtBytes) if err != nil { |