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 {...@@ -772,6 +772,10 @@ pub const Tokenizer = struct {
772 },772 },
773773
774 .char_literal_unicode_escape_saw_u => switch (c) {774 .char_literal_unicode_escape_saw_u => switch (c) {
775 0 => {
776 result.tag = .invalid;
777 break;
778 },
775 '{' => {779 '{' => {
776 state = .char_literal_unicode_escape;780 state = .char_literal_unicode_escape;
777 },781 },
...@@ -782,6 +786,10 @@ pub const Tokenizer = struct {...@@ -782,6 +786,10 @@ pub const Tokenizer = struct {
782 },786 },
783787
784 .char_literal_unicode_escape => switch (c) {788 .char_literal_unicode_escape => switch (c) {
789 0 => {
790 result.tag = .invalid;
791 break;
792 },
785 '0'...'9', 'a'...'f', 'A'...'F' => {},793 '0'...'9', 'a'...'f', 'A'...'F' => {},
786 '}' => {794 '}' => {
787 state = .char_literal_end; // too many/few digits handled later795 state = .char_literal_end; // too many/few digits handled later
...@@ -1922,8 +1930,10 @@ test "tokenizer - invalid builtin identifiers" {...@@ -1922,8 +1930,10 @@ test "tokenizer - invalid builtin identifiers" {
1922 try testTokenize("@0()", &.{ .invalid, .integer_literal, .l_paren, .r_paren });1930 try testTokenize("@0()", &.{ .invalid, .integer_literal, .l_paren, .r_paren });
1923}1931}
19241932
1925test "tokenizer - backslash before eof in string literal" {1933test "tokenizer - invalid token with unfinished escape right before eof" {
1926 try testTokenize("\"\\", &.{.invalid});1934 try testTokenize("\"\\", &.{.invalid});
1935 try testTokenize("'\\", &.{.invalid});
1936 try testTokenize("'\\u", &.{.invalid});
1927}1937}
19281938
1929fn testTokenize(source: [:0]const u8, expected_tokens: []const Token.Tag) !void {1939fn testTokenize(source: [:0]const u8, expected_tokens: []const Token.Tag) !void {