Skip to content

Commit

Permalink
go/scanner: fall back to next() when encountering 0 bytes in parseIde…
Browse files Browse the repository at this point in the history
…ntifier

CL 308611 optimized parseIdentifier for ASCII, but inadvertently skipped
error handling for 0 bytes. Don't take the optimized path when
encountering 0.

Fixes #46855

Change-Id: Ic584e077eb74c012611fefa20eb71ca09c81b3c7
Reviewed-on: https://go-review.googlesource.com/c/go/+/329790
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
findleyr committed Jun 21, 2021
1 parent 44f9a35 commit 20bdfba
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/go/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (s *Scanner) scanIdentifier() string {
continue
}
s.rdOffset += rdOffset
if b < utf8.RuneSelf {
if 0 < b && b < utf8.RuneSelf {
// Optimization: we've encountered an ASCII character that's not a letter
// or number. Avoid the call into s.next() and corresponding set up.
//
Expand Down
2 changes: 2 additions & 0 deletions src/go/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ var errors = []struct {
{"//\ufeff", token.COMMENT, 2, "//\ufeff", "illegal byte order mark"}, // only first BOM is ignored
{"'\ufeff" + `'`, token.CHAR, 1, "'\ufeff" + `'`, "illegal byte order mark"}, // only first BOM is ignored
{`"` + "abc\ufeffdef" + `"`, token.STRING, 4, `"` + "abc\ufeffdef" + `"`, "illegal byte order mark"}, // only first BOM is ignored
{"abc\x00def", token.IDENT, 3, "abc", "illegal character NUL"},
{"abc\x00", token.IDENT, 3, "abc", "illegal character NUL"},
}

func TestScanErrors(t *testing.T) {
Expand Down

0 comments on commit 20bdfba

Please sign in to comment.