1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:04:57 -07:00

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>
This commit is contained in:
Rebecca Stambler 2020-04-09 13:59:17 -04:00
parent e3f0bd94ad
commit 79a7a3126e

View File

@ -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