From b346f7fd45de50e7e5dd5d24a7a009c3c8d8ec61 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Wed, 7 Aug 2019 23:49:42 +0200 Subject: [PATCH] x/tools/cmd/gopls: prevent nil pointer dereferences The `files` slice is used twice. First it's used to get all results from `ph.Parse`, and then it's reused to filter all `nil` values (which may have been returned by the `ph.Parse` method). After the loop to "filter" out all the `nil` values, we also need to strip the remaining values. I also changed the ordering so that we first check the errors and only then perform this loop. That way the code will return earlier when the context was canceled. Partially fixes #33531 by prevention the panic reported in that issue. Change-Id: I09478e765adcd0384ec4745921eb5c5aea405ef2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/189397 Reviewed-by: Rebecca Stambler Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/cache/check.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go index c571cf5c83..3bc6f05ad2 100644 --- a/internal/lsp/cache/check.go +++ b/internal/lsp/cache/check.go @@ -146,13 +146,6 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error) } wg.Wait() - var i int - for _, f := range files { - if f != nil { - files[i] = f - i++ - } - } for _, err := range parseErrors { if err == context.Canceled { return nil, err @@ -162,6 +155,15 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error) } } + var i int + for _, f := range files { + if f != nil { + files[i] = f + i++ + } + } + files = files[:i] + // Use the default type information for the unsafe package. if meta.pkgPath == "unsafe" { pkg.types = types.Unsafe