diff options
author | Rebecca Stambler <rstambler@golang.org> | 2020-04-09 13:59:17 -0400 |
---|---|---|
committer | Rebecca Stambler <rstambler@golang.org> | 2020-04-10 19:49:07 +0000 |
commit | 79a7a3126eef9c5f4ff7a1580f8e0d45e0e85e77 (patch) | |
tree | 70f66b2e7f0c2f8f9da59b68cce0769abef51677 /internal/lsp | |
parent | e3f0bd94ad67cbc437dcadd24ba3519638850d5d (diff) | |
download | golang-x-tools-79a7a3126eef9c5f4ff7a1580f8e0d45e0e85e77.tar.gz |
internal/lsp: add an extra bounds check to avoid nil pointers
A nil pointer was reported to the golang-tools group (see
https://groups.google.com/g/golang-tools/c/JrNTz8I6ifo/m/tcJRpek-AAAJ).
I think this bounds check should address it.
Updates golang/go#34433
Change-Id: I87352c269c65c844c86ebe9ee3fd2d041cc49ee9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227770
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'internal/lsp')
-rw-r--r-- | internal/lsp/source/format.go | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/internal/lsp/source/format.go b/internal/lsp/source/format.go index 06d0f7bf6..6240c69bd 100644 --- a/internal/lsp/source/format.go +++ b/internal/lsp/source/format.go @@ -245,9 +245,12 @@ func trimToImports(fset *token.FileSet, f *ast.File, src []byte) ([]byte, int) { if nextLine := fset.Position(end).Line + 1; tok.LineCount() >= nextLine { end = fset.File(f.Pos()).LineStart(nextLine) } + if start > end { + return nil, 0 + } startLineOffset := fset.Position(start).Line - 1 // lines are 1-indexed. - return src[fset.Position(firstImport.Pos()).Offset:fset.Position(end).Offset], startLineOffset + return src[fset.Position(start).Offset:fset.Position(end).Offset], startLineOffset } // trimToFirstNonImport returns src from the beginning to the first non-import |