1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07:00

internal/span: catch potential panics in go/token

Updates golang/go#32639

Change-Id: Ib413fc316af4308d8928647ff36a437d243a9c11
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184038
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-06-27 12:39:54 -04:00
parent 043e3d946a
commit 4e38253394

View File

@ -99,6 +99,14 @@ func (s Span) Range(converter *TokenConverter) (Range, error) {
if err != nil { if err != nil {
return Range{}, err return Range{}, err
} }
// go/token will panic if the offset is larger than the file's size,
// so check here to avoid panicking.
if s.Start().Offset() > converter.file.Size() {
return Range{}, fmt.Errorf("start offset %v is past the end of the file", s.Start())
}
if s.End().Offset() > converter.file.Size() {
return Range{}, fmt.Errorf("end offset %v is past the end of the file", s.End())
}
return Range{ return Range{
FileSet: converter.fset, FileSet: converter.fset,
Start: converter.file.Pos(s.Start().Offset()), Start: converter.file.Pos(s.Start().Offset()),
@ -107,7 +115,9 @@ func (s Span) Range(converter *TokenConverter) (Range, error) {
} }
func (l *TokenConverter) ToPosition(offset int) (int, int, error) { func (l *TokenConverter) ToPosition(offset int) (int, int, error) {
//TODO: check offset fits in file if offset > l.file.Size() {
return 0, 0, fmt.Errorf("offset %v is past the end of the file", offset)
}
pos := l.file.Pos(offset) pos := l.file.Pos(offset)
p := l.fset.Position(pos) p := l.fset.Position(pos)
return p.Line, p.Column, nil return p.Line, p.Column, nil