diff options
author | Russ Cox <rsc@golang.org> | 2014-11-05 22:50:24 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2014-11-05 22:50:24 -0500 |
commit | 15a5c8ec44a8db796713e0d61ea998842a001f5b (patch) | |
tree | bd06b71f61c3278f74fc71976f02490468bc589d /src | |
parent | 7fead16b007b5a26116459cdf3c0bee5835a54d1 (diff) | |
download | go-15a5c8ec44a8db796713e0d61ea998842a001f5b.tar.gz |
bufio: fix reading of many blank lines in a row
Fixes issue 9020.
LGTM=bradfitz, r
R=r, bradfitz
CC=golang-codereviews
https://codereview.appspot.com/170030043
Diffstat (limited to 'src')
-rw-r--r-- | src/bufio/scan.go | 3 | ||||
-rw-r--r-- | src/bufio/scan_test.go | 12 |
2 files changed, 14 insertions, 1 deletions
diff --git a/src/bufio/scan.go b/src/bufio/scan.go index 73ad763b8..364d15961 100644 --- a/src/bufio/scan.go +++ b/src/bufio/scan.go @@ -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") diff --git a/src/bufio/scan_test.go b/src/bufio/scan_test.go index a1cf90ddb..bf888dafb 100644 --- a/src/bufio/scan_test.go +++ b/src/bufio/scan_test.go @@ -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) { |