]> Cypherpunks repositories - gostls13.git/commitdiff
text/scanner: guard against installed IsIdentRune that accepts EOF
authorRobert Griesemer <gri@golang.org>
Wed, 16 Mar 2022 04:44:37 +0000 (21:44 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 17 Mar 2022 03:41:50 +0000 (03:41 +0000)
IsIdentRune may be installed by a client of the scanner. If the
installed function accepts EOF as a valid identifier rune, Scan
calls may not terminate.

Check for EOF when a user-defined IsIdentRune is used.

Fixes #50909.

Change-Id: Ib104b03ee59e2d58faa71f227c3b51ba424f7f61
Reviewed-on: https://go-review.googlesource.com/c/go/+/393254
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/text/scanner/scanner.go
src/text/scanner/scanner_test.go

index f1fbf9861d06bff843450a1a277efbf94d03b6b5..735982afcb2ebd31557af5d6f2c94724547a234e 100644 (file)
@@ -346,7 +346,7 @@ func (s *Scanner) errorf(format string, args ...any) {
 
 func (s *Scanner) isIdentRune(ch rune, i int) bool {
        if s.IsIdentRune != nil {
-               return s.IsIdentRune(ch, i)
+               return ch != EOF && s.IsIdentRune(ch, i)
        }
        return ch == '_' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
 }
index fe39d3060bf185a171aacc5ee527c7d4879582db..6a454d9be77ba618cac70d40bce922f3f9b20ba9 100644 (file)
@@ -913,3 +913,22 @@ func extractInts(t string, mode uint) (res string) {
                }
        }
 }
+
+func TestIssue50909(t *testing.T) {
+       var s Scanner
+       s.Init(strings.NewReader("hello \n\nworld\n!\n"))
+       s.IsIdentRune = func(ch rune, _ int) bool { return ch != '\n' }
+
+       r := ""
+       n := 0
+       for s.Scan() != EOF && n < 10 {
+               r += s.TokenText()
+               n++
+       }
+
+       const R = "hello world!"
+       const N = 3
+       if r != R || n != N {
+               t.Errorf("got %q (n = %d); want %q (n = %d)", r, n, R, N)
+       }
+}