Skip to content

Commit

Permalink
text/scanner: guard against installed IsIdentRune that accepts EOF
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
griesemer committed Mar 17, 2022
1 parent 8427429 commit 9956a54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/text/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions src/text/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 9956a54

Please sign in to comment.