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>
diff --git a/internal/lsp/cache/parse.go b/internal/lsp/cache/parse.go
index 742f48f..fc110c7 100644
--- a/internal/lsp/cache/parse.go
+++ b/internal/lsp/cache/parse.go
@@ -995,6 +995,10 @@
 	}
 
 	// 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 {