]> Cypherpunks repositories - gostls13.git/commitdiff
text/scanner: avoid further reads after EOF
authorRobert Griesemer <gri@golang.org>
Wed, 13 May 2015 23:07:33 +0000 (16:07 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 13 May 2015 23:14:50 +0000 (23:14 +0000)
Fixes #10735.

Change-Id: I5c6e424653657c89da176136ac56597c7565abe5
Reviewed-on: https://go-review.googlesource.com/10039
Reviewed-by: Rob Pike <r@golang.org>
src/text/scanner/scanner.go
src/text/scanner/scanner_test.go

index d3eadfd7e18d48e1c92fc93adda8d731753488cf..eacc0a2245c4f566c94092801488900e0b41528c 100644 (file)
@@ -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('"')
index aca17b1b275b6b6b9f544713ae3cfea80b606f20..798bed7e92aa0aa0c17e53e81022d64e79f0a03d 100644 (file)
@@ -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)
        }
 }