]> Cypherpunks repositories - gostls13.git/commitdiff
bufio/Scan: fix error handling at EOF
authorRob Pike <r@golang.org>
Thu, 11 Apr 2013 03:58:19 +0000 (20:58 -0700)
committerRob Pike <r@golang.org>
Thu, 11 Apr 2013 03:58:19 +0000 (20:58 -0700)
Fixes #5268.

R=golang-dev, dsymonds, bradfitz
CC=golang-dev
https://golang.org/cl/8646045

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

index 486853e6bc06fc3bd25e6f1a1dba50572703ba5b..cebe92d331e6af1af37fb5d13df052d69d9ea0da 100644 (file)
@@ -103,7 +103,8 @@ func (s *Scanner) Text() string {
 
 // Scan advances the Scanner to the next token, which will then be
 // available through the Bytes or Text method. It returns false when the
-// scan stops, either by reaching the end of the input or an error.
+// scan stops, either by reaching the end of the input, a zero-length
+// read from the input, or an error.
 // After Scan returns false, the Err method will return any error that
 // occurred during scanning, except that if it was io.EOF, Err
 // will return nil.
@@ -164,7 +165,7 @@ func (s *Scanner) Scan() bool {
                        s.setErr(err)
                }
                if n == 0 { // Don't loop forever if Reader doesn't deliver EOF.
-                       s.err = io.EOF
+                       s.setErr(io.EOF)
                }
                s.end += n
        }
index 48729aabb1173a9cb67ca550a4e30e59fdd98876..1b112f46dabad8bd487621c8e635f3d572984e47 100644 (file)
@@ -368,3 +368,21 @@ func TestErrAtEOF(t *testing.T) {
                t.Fatal("wrong error:", s.Err())
        }
 }
+
+// Test for issue 5268.
+type alwaysError struct{}
+
+func (alwaysError) Read(p []byte) (int, error) {
+       return 0, io.ErrUnexpectedEOF
+}
+
+func TestNonEOFWithEmptyRead(t *testing.T) {
+       scanner := NewScanner(alwaysError{})
+       for scanner.Scan() {
+               t.Fatal("read should fail")
+       }
+       err := scanner.Err()
+       if err != io.ErrUnexpectedEOF {
+               t.Errorf("unexpected error: %v", err)
+       }
+}