Commit 2d0db8e5 authored by Russ Cox's avatar Russ Cox

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
parent 67742ef5
...@@ -128,9 +128,10 @@ func (s *Scanner) Scan() bool { ...@@ -128,9 +128,10 @@ func (s *Scanner) Scan() bool {
} }
s.token = token s.token = token
if token != nil { if token != nil {
if len(token) > 0 { if s.err == nil || advance > 0 {
s.empties = 0 s.empties = 0
} else { } else {
// Returning tokens not advancing input at EOF.
s.empties++ s.empties++
if s.empties > 100 { if s.empties > 100 {
panic("bufio.Scan: 100 empty tokens without progressing") panic("bufio.Scan: 100 empty tokens without progressing")
......
...@@ -489,6 +489,18 @@ func TestDontLoopForever(t *testing.T) { ...@@ -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 type countdown int
func (c *countdown) split(data []byte, atEOF bool) (advance int, token []byte, err error) { func (c *countdown) split(data []byte, atEOF bool) (advance int, token []byte, err error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment