Commit fd5b8aa7 authored by Robert Griesemer's avatar Robert Griesemer

text/scanner: avoid further reads after EOF

Fixes #10735.

Change-Id: I5c6e424653657c89da176136ac56597c7565abe5
Reviewed-on: https://go-review.googlesource.com/10039Reviewed-by: default avatarRob Pike <r@golang.org>
parent f8fbcefa
......@@ -314,7 +314,9 @@ func (s *Scanner) Next() rune {
s.tokPos = -1 // don't collect token text
s.Line = 0 // invalidate token position
ch := s.Peek()
s.ch = s.next()
if ch != EOF {
s.ch = s.next()
}
return ch
}
......@@ -597,6 +599,8 @@ redo:
}
default:
switch ch {
case EOF:
break
case '"':
if s.Mode&ScanStrings != 0 {
s.scanString('"')
......
......@@ -619,13 +619,12 @@ func TestPos(t *testing.T) {
type countReader int
func (c *countReader) Read([]byte) (int, error) {
*c++
func (r *countReader) Read([]byte) (int, error) {
*r++
return 0, io.EOF
}
func TestPeekEOFHandling(t *testing.T) {
func TestNextEOFHandling(t *testing.T) {
var r countReader
// corner case: empty source
......@@ -633,15 +632,36 @@ func TestPeekEOFHandling(t *testing.T) {
tok := s.Next()
if tok != EOF {
t.Errorf("EOF not reported")
t.Error("1) EOF not reported")
}
tok = s.Peek()
if tok != EOF {
t.Error("2) EOF not reported")
}
if r != 1 {
t.Errorf("scanner called Read %d times, not once", r)
}
}
func TestScanEOFHandling(t *testing.T) {
var r countReader
// corner case: empty source
s := new(Scanner).Init(&r)
tok := s.Scan()
if tok != EOF {
t.Error("1) EOF not reported")
}
tok = s.Peek()
if tok != EOF {
t.Errorf("EOF not reported")
t.Error("2) EOF not reported")
}
if r != 2 {
t.Errorf("scanner called Read %d times, not twice", r)
if r != 1 {
t.Errorf("scanner called Read %d times, not once", r)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment