aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2014-11-05 22:50:24 -0500
committerRuss Cox <rsc@golang.org>2014-11-05 22:50:24 -0500
commit2d0db8e591513a1123057b8c330c946ddcb4fbc8 (patch)
tree887fa345faf178b4666ec567a3ed8129f54a1ed2
parent67742ef560be186a2ebeb45aaab50399bf81b358 (diff)
downloadgo-2d0db8e591513a1123057b8c330c946ddcb4fbc8.tar.gz
go-2d0db8e591513a1123057b8c330c946ddcb4fbc8.zip
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
-rw-r--r--src/bufio/scan.go3
-rw-r--r--src/bufio/scan_test.go12
2 files changed, 14 insertions, 1 deletions
diff --git a/src/bufio/scan.go b/src/bufio/scan.go
index 73ad763b8f..364d159613 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 a1cf90ddbf..bf888dafb5 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) {