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 { ...@@ -314,7 +314,9 @@ func (s *Scanner) Next() rune {
s.tokPos = -1 // don't collect token text s.tokPos = -1 // don't collect token text
s.Line = 0 // invalidate token position s.Line = 0 // invalidate token position
ch := s.Peek() ch := s.Peek()
if ch != EOF {
s.ch = s.next() s.ch = s.next()
}
return ch return ch
} }
...@@ -597,6 +599,8 @@ redo: ...@@ -597,6 +599,8 @@ redo:
} }
default: default:
switch ch { switch ch {
case EOF:
break
case '"': case '"':
if s.Mode&ScanStrings != 0 { if s.Mode&ScanStrings != 0 {
s.scanString('"') s.scanString('"')
......
...@@ -619,13 +619,12 @@ func TestPos(t *testing.T) { ...@@ -619,13 +619,12 @@ func TestPos(t *testing.T) {
type countReader int type countReader int
func (c *countReader) Read([]byte) (int, error) { func (r *countReader) Read([]byte) (int, error) {
*c++ *r++
return 0, io.EOF return 0, io.EOF
} }
func TestPeekEOFHandling(t *testing.T) { func TestNextEOFHandling(t *testing.T) {
var r countReader var r countReader
// corner case: empty source // corner case: empty source
...@@ -633,15 +632,36 @@ func TestPeekEOFHandling(t *testing.T) { ...@@ -633,15 +632,36 @@ func TestPeekEOFHandling(t *testing.T) {
tok := s.Next() tok := s.Next()
if tok != EOF { 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() tok = s.Peek()
if tok != EOF { if tok != EOF {
t.Errorf("EOF not reported") t.Error("2) EOF not reported")
} }
if r != 2 { if r != 1 {
t.Errorf("scanner called Read %d times, not twice", r) 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