Skip to content

Commit 0ccc765

Browse files
committed
Ripper: Fix duplicate regexp errors
1 parent fb18bb1 commit 0ccc765

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

parse.y

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13186,7 +13186,13 @@ new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
1318613186
nd_set_loc(node, loc);
1318713187
rb_node_dregx_t *const dreg = RNODE_DREGX(node);
1318813188
dreg->as.nd_cflag = options & RE_OPTION_MASK;
13189-
if (dreg->string) reg_fragment_check(p, dreg->string, options);
13189+
if (!dreg->nd_next) {
13190+
/* Check string is valid regex */
13191+
reg_compile(p, dreg->string, options);
13192+
}
13193+
else if (dreg->string) {
13194+
reg_fragment_check(p, dreg->string, options);
13195+
}
1319013196
prev = node;
1319113197
for (list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
1319213198
NODE *frag = list->nd_head;
@@ -13212,10 +13218,6 @@ new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
1321213218
prev = 0;
1321313219
}
1321413220
}
13215-
if (!dreg->nd_next) {
13216-
/* Check string is valid regex */
13217-
reg_compile(p, dreg->string, options);
13218-
}
1321913221
if (options & RE_OPTION_ONCE) {
1322013222
node = NEW_ONCE(node, loc);
1322113223
}

test/ripper/test_ripper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ def test_assignable_in_regexp
156156
end
157157

158158
def test_invalid_multibyte_character_in_regexp
159-
lex = Ripper.lex(%q[/#{"\xcd"}/])
159+
lex = Ripper::Lexer.new(%q[/#{"\xcd"}/]).scan.map(&:to_a)
160160
assert_equal([[1, 0], :on_regexp_beg, "/", state(:EXPR_BEG)], lex.shift)
161161
assert_equal([[1, 1], :on_embexpr_beg, "\#{", state(:EXPR_BEG)], lex.shift)
162162
assert_equal([[1, 3], :on_tstring_beg, "\"", state(:EXPR_BEG)], lex.shift)
163163
assert_equal([[1, 4], :on_tstring_content, "\\xcd", state(:EXPR_BEG)], lex.shift)
164164
assert_equal([[1, 8], :on_tstring_end, "\"", state(:EXPR_END)], lex.shift)
165165
assert_equal([[1, 9], :on_embexpr_end, "}", state(:EXPR_END)], lex.shift)
166166
assert_equal([[1, 10], :on_regexp_end, "/", state(:EXPR_BEG)], lex.shift)
167+
assert_equal([[1, 11], :compile_error, "", state(:EXPR_END), "invalid multibyte character: /\\xCD/"], lex.shift)
167168
assert_empty(lex)
168169
end
169170

0 commit comments

Comments
 (0)