Skip to content

Commit 5e0a53b

Browse files
committed
[Bug #20990] Fix tokenizing "\M-\[mbchar]" and "\C-\[mbchar]"
1 parent 0ccc765 commit 5e0a53b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

parse.y

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8152,11 +8152,15 @@ read_escape(struct parser_params *p, int flags, const char *begin)
81528152
goto eof;
81538153
}
81548154
if ((c = nextc(p)) == '\\') {
8155-
switch (peekc(p)) {
8155+
switch (c = peekc(p)) {
81568156
case 'u': case 'U':
81578157
nextc(p);
81588158
goto eof;
81598159
}
8160+
if (!ISASCII(c) && c != -1) {
8161+
tokskip_mbchar(p);
8162+
goto eof;
8163+
}
81608164
return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
81618165
}
81628166
else if (c == -1) goto eof;
@@ -8185,11 +8189,15 @@ read_escape(struct parser_params *p, int flags, const char *begin)
81858189
case 'c':
81868190
if (flags & ESCAPE_CONTROL) goto eof;
81878191
if ((c = nextc(p))== '\\') {
8188-
switch (peekc(p)) {
8192+
switch (c = peekc(p)) {
81898193
case 'u': case 'U':
81908194
nextc(p);
81918195
goto eof;
81928196
}
8197+
if (!ISASCII(c) && c != -1) {
8198+
tokskip_mbchar(p);
8199+
goto eof;
8200+
}
81938201
c = read_escape(p, flags|ESCAPE_CONTROL, begin);
81948202
}
81958203
else if (c == '?')

test/ripper/test_lexer.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,30 @@ def test_invalid_escape_ctrl_meta_mbchar
390390
assert_lexer(expected, code)
391391
end
392392

393+
def test_invalid_escape_ctrl_backslash_mbchar
394+
code = %["\\C-\\\u{3042}"]
395+
expected = [
396+
[[1, 0], :on_tstring_beg, '"', state(:EXPR_BEG)],
397+
[[1, 1], :on_tstring_content, "\\C-\\", state(:EXPR_BEG)],
398+
[[1, 5], :on_tstring_content, "\u{3042}", state(:EXPR_BEG)],
399+
[[1, 8], :on_tstring_end, '"', state(:EXPR_END)],
400+
]
401+
402+
assert_lexer(expected, code)
403+
end
404+
405+
def test_invalid_escape_meta_backslash_mbchar
406+
code = %["\\M-\\\u{3042}"]
407+
expected = [
408+
[[1, 0], :on_tstring_beg, '"', state(:EXPR_BEG)],
409+
[[1, 1], :on_tstring_content, "\\M-\\", state(:EXPR_BEG)],
410+
[[1, 5], :on_tstring_content, "\u{3042}", state(:EXPR_BEG)],
411+
[[1, 8], :on_tstring_end, '"', state(:EXPR_END)],
412+
]
413+
414+
assert_lexer(expected, code)
415+
end
416+
393417
def test_invalid_escape_string
394418
code = "\"hello\\x world"
395419
expected = [

0 commit comments

Comments
 (0)