bufio: fix reading of many blank lines in a row

Fixes #9020.

LGTM=bradfitz, r
R=r, bradfitz
CC=golang-codereviews
https://golang.org/cl/170030043
This commit is contained in:
Russ Cox 2014-11-05 22:50:24 -05:00
parent 67742ef560
commit 2d0db8e591
2 changed files with 14 additions and 1 deletions

View File

@ -128,9 +128,10 @@ func (s *Scanner) Scan() bool {
}
s.token = token
if token != nil {
if len(token) > 0 {
if s.err == nil || advance > 0 {
s.empties = 0
} else {
// Returning tokens not advancing input at EOF.
s.empties++
if s.empties > 100 {
panic("bufio.Scan: 100 empty tokens without progressing")

View File

@ -489,6 +489,18 @@ func TestDontLoopForever(t *testing.T) {
}
}
func TestBlankLines(t *testing.T) {
s := NewScanner(strings.NewReader(strings.Repeat("\n", 1000)))
for count := 0; s.Scan(); count++ {
if count > 2000 {
t.Fatal("looping")
}
}
if s.Err() != nil {
t.Fatal("after scan:", s.Err())
}
}
type countdown int
func (c *countdown) split(data []byte, atEOF bool) (advance int, token []byte, err error) {