mirror of https://github.com/golang/go.git
internal/lsp/source: trim file very carefully
Apparently the AST will sometimes give us offsets past the end of the file. Don't crash when it does. Fixes golang/go#36610. Change-Id: I3cfbf8645bfcea94a5d87bca5bef4236d657b2c0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/215119 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
a20955124b
commit
95ece921ff
|
|
@ -245,8 +245,13 @@ func trimToImports(fset *token.FileSet, f *ast.File, src []byte) ([]byte, int) {
|
|||
tok := fset.File(f.Pos())
|
||||
start := firstImport.Pos()
|
||||
end := lastImport.End()
|
||||
if tok.LineCount() > fset.Position(end).Line {
|
||||
end = fset.File(f.Pos()).LineStart(fset.Position(lastImport.End()).Line + 1)
|
||||
// The parser will happily feed us nonsense. See golang/go#36610.
|
||||
tokStart, tokEnd := token.Pos(tok.Base()), token.Pos(tok.Base()+tok.Size())
|
||||
if start < tokStart || start > tokEnd || end < tokStart || end > tokEnd {
|
||||
return nil, 0
|
||||
}
|
||||
if nextLine := fset.Position(end).Line + 1; tok.LineCount() >= nextLine {
|
||||
end = fset.File(f.Pos()).LineStart(nextLine)
|
||||
}
|
||||
|
||||
startLineOffset := fset.Position(start).Line - 1 // lines are 1-indexed.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTrimToImports(t *testing.T) {
|
||||
const input = `package source
|
||||
|
||||
import (
|
||||
m
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func foo() {
|
||||
fmt.Println("hi")
|
||||
}
|
||||
`
|
||||
|
||||
fs := token.NewFileSet()
|
||||
f, _ := parser.ParseFile(fs, "foo.go", input, parser.ImportsOnly)
|
||||
trimToImports(fs, f, []byte(input))
|
||||
}
|
||||
Loading…
Reference in New Issue