]> Cypherpunks repositories - gostls13.git/commitdiff
bufio: fix reading of many blank lines in a row
authorRuss Cox <rsc@golang.org>
Thu, 6 Nov 2014 03:50:24 +0000 (22:50 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 6 Nov 2014 03:50:24 +0000 (22:50 -0500)
Fixes #9020.

LGTM=bradfitz, r
R=r, bradfitz
CC=golang-codereviews
https://golang.org/cl/170030043

src/bufio/scan.go
src/bufio/scan_test.go

index 73ad763b8f2171bb65ca54c1e86c1f0bb2d84738..364d15961399b6a07e56f7bf4e0ae7e06b3ec33d 100644 (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")
index a1cf90ddbfca11926cd50738ab06258e44768231..bf888dafb585618c2c7f10f7325a53697a6cf1e5 100644 (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) {