Skip to content

Commit f980c94

Browse files
jhawthornk0kubun
authored andcommitted
Fix reuse of MatchData assigned to $~
$~ = m = /a/.match("a").dup /b/ =~ "b" p m [Bug #22381] (cherry picked from commit ea3cd7e)
1 parent 9554389 commit f980c94

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎re.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,6 +4622,7 @@ match_setter(VALUE val, ID _x, VALUE *_y)
46224622
{
46234623
if (!NIL_P(val)) {
46244624
Check_Type(val, T_MATCH);
4625+
rb_match_busy(val);
46254626
}
46264627
rb_backref_set(val);
46274628
}

‎test/ruby/test_regexp.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,19 @@ def test_match_setter
10251025
assert_equal("foo", $&)
10261026
end
10271027

1028+
def test_match_setter_with_copy
1029+
[:dup, :clone].each do |copy|
1030+
[false, true].each do |freeze|
1031+
m = /a/.match("a").public_send(copy)
1032+
m.freeze if freeze
1033+
$~ = m
1034+
/b/ =~ "b"
1035+
assert_equal("a", m[0], "#{copy}, freeze: #{freeze}")
1036+
assert_equal("b", $&)
1037+
end
1038+
end
1039+
end
1040+
10281041
def test_match_without_regexp
10291042
# create a MatchData for each assertion because the internal state may change
10301043
test = proc {|&blk| "abc".sub("a", ""); blk.call($~) }

0 commit comments

Comments
 (0)