From ab4c7077cc44cd6725625562b7380a44cf462190 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 1 Dec 2022 01:31:24 +0900 Subject: Prevent segfault in String#scan with ObjectSpace.each_object Calling `String#scan` without a block creates an incomplete MatchData object whose `RMATCH(match)->str` is Qfalse. Usually this object is not leaked, but it was possible to pull it by using ObjectSpace.each_object. This change hides the internal MatchData object by using rb_obj_hide. Fixes [Bug #19159] --- test/ruby/test_string.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 66db5b565d..1ece47b18a 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1601,6 +1601,15 @@ CODE assert_equal(%w[1 2 3], S("a1 a2 a3").scan(/a\K./)) end + def test_scan_segv + bug19159 = '[Bug #19159]' + assert_nothing_raised(Exception, bug19159) do + ObjectSpace.each_object(MatchData).to_a + "".scan(//) + ObjectSpace.each_object(MatchData).to_a.inspect + end + end + def test_size assert_equal(0, S("").size) assert_equal(4, S("1234").size) -- cgit v1.2.3