authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2021-09-20 18:00:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-22 14:33:33-04:00
log3b09262c1252de0d9f946630e701be65bc5b2fc7
tree12f0bf4c988c59679fae45c8ce25ebf6b556a4ba
parente14fcd60cb11d731ca19f97e5b0b6247aa7cf07b

tokenizer: Fix index-out-of-bounds on unfinished unicode escapes before EOF


1 files changed, 11 insertions(+), 1 deletions(-)

lib/std/zig/tokenizer.zig+11-1
......@@ -772,6 +772,10 @@ pub const Tokenizer = struct {
772772 },
773773
774774 .char_literal_unicode_escape_saw_u => switch (c) {
775 0 => {
776 result.tag = .invalid;
777 break;
778 },
775779 '{' => {
776780 state = .char_literal_unicode_escape;
777781 },
......@@ -782,6 +786,10 @@ pub const Tokenizer = struct {
782786 },
783787
784788 .char_literal_unicode_escape => switch (c) {
789 0 => {
790 result.tag = .invalid;
791 break;
792 },
785793 '0'...'9', 'a'...'f', 'A'...'F' => {},
786794 '}' => {
787795 state = .char_literal_end; // too many/few digits handled later
......@@ -1922,8 +1930,10 @@ test "tokenizer - invalid builtin identifiers" {
19221930 try testTokenize("@0()", &.{ .invalid, .integer_literal, .l_paren, .r_paren });
19231931}
19241932
1925test "tokenizer - backslash before eof in string literal" {
1933test "tokenizer - invalid token with unfinished escape right before eof" {
19261934 try testTokenize("\"\\", &.{.invalid});
1935 try testTokenize("'\\", &.{.invalid});
1936 try testTokenize("'\\u", &.{.invalid});
19271937}
19281938
19291939fn testTokenize(source: [:0]const u8, expected_tokens: []const Token.Tag) !void {