Skip to content

Commit 79a7a31

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e3f0bd9 commit 79a7a31

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

internal/lsp/source/format.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,12 @@ func trimToImports(fset *token.FileSet, f *ast.File, src []byte) ([]byte, int) {
245245
if nextLine := fset.Position(end).Line + 1; tok.LineCount() >= nextLine {
246246
end = fset.File(f.Pos()).LineStart(nextLine)
247247
}
248+
if start > end {
249+
return nil, 0
250+
}
248251

249252
startLineOffset := fset.Position(start).Line - 1 // lines are 1-indexed.
250-
return src[fset.Position(firstImport.Pos()).Offset:fset.Position(end).Offset], startLineOffset
253+
return src[fset.Position(start).Offset:fset.Position(end).Offset], startLineOffset
251254
}
252255

253256
// trimToFirstNonImport returns src from the beginning to the first non-import

0 commit comments

Comments
 (0)