Skip to content

Commit 082a4a8

Browse files
committed
bufio/Scan: fix error handling at EOF
Fixes golang#5268. R=golang-dev, dsymonds, bradfitz CC=golang-dev https://golang.org/cl/8646045
1 parent 7ac2085 commit 082a4a8

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/pkg/bufio/scan.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ func (s *Scanner) Text() string {
103103

104104
// Scan advances the Scanner to the next token, which will then be
105105
// available through the Bytes or Text method. It returns false when the
106-
// scan stops, either by reaching the end of the input or an error.
106+
// scan stops, either by reaching the end of the input, a zero-length
107+
// read from the input, or an error.
107108
// After Scan returns false, the Err method will return any error that
108109
// occurred during scanning, except that if it was io.EOF, Err
109110
// will return nil.
@@ -164,7 +165,7 @@ func (s *Scanner) Scan() bool {
164165
s.setErr(err)
165166
}
166167
if n == 0 { // Don't loop forever if Reader doesn't deliver EOF.
167-
s.err = io.EOF
168+
s.setErr(io.EOF)
168169
}
169170
s.end += n
170171
}

src/pkg/bufio/scan_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,21 @@ func TestErrAtEOF(t *testing.T) {
368368
t.Fatal("wrong error:", s.Err())
369369
}
370370
}
371+
372+
// Test for issue 5268.
373+
type alwaysError struct{}
374+
375+
func (alwaysError) Read(p []byte) (int, error) {
376+
return 0, io.ErrUnexpectedEOF
377+
}
378+
379+
func TestNonEOFWithEmptyRead(t *testing.T) {
380+
scanner := NewScanner(alwaysError{})
381+
for scanner.Scan() {
382+
t.Fatal("read should fail")
383+
}
384+
err := scanner.Err()
385+
if err != io.ErrUnexpectedEOF {
386+
t.Errorf("unexpected error: %v", err)
387+
}
388+
}

0 commit comments

Comments
 (0)