Skip to content

Commit 7db0202

Browse files
committed
[Bug #22603] Fix off-by-one in named-capture local assignment
The longest name, `__ENCODING__`, has not been skipped.
1 parent 3288e45 commit 7db0202

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎parse.y‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15583,7 +15583,7 @@ rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, lo
1558315583
return ST_CONTINUE;
1558415584

1558515585
var = intern_cstr(s, len, enc);
15586-
if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15586+
if (len <= MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
1558715587
if (!lvar_defined(p, var)) return ST_CONTINUE;
1558815588
}
1558915589
node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);

‎test/ruby/test_regexp.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,20 @@ def (obj = Object.new).test(s, nil: :ng)
368368
/(?<nil>.)/ =~ "c"
369369
assert_equal("c", binding.local_variable_get(:nil))
370370
end
371+
372+
/(?<__ENCODING__>.)/ =~ "a"
373+
assert_not_include(local_variables, :__ENCODING__)
374+
375+
def (obj = Object.new).test(s, __ENCODING__: :ng)
376+
/(?<__ENCODING__>.)/ =~ s
377+
binding.local_variable_get(:__ENCODING__)
378+
end
379+
assert_equal("b", obj.test("b"))
380+
381+
tap do |__ENCODING__: :ng|
382+
/(?<__ENCODING__>.)/ =~ "c"
383+
assert_equal("c", binding.local_variable_get(:__ENCODING__))
384+
end
371385
end
372386

373387
def test_assign_named_capture_to_const

0 commit comments

Comments
 (0)