ruby-changes:74420
From: TSUYUSATO <ko1@a...>
Date: Wed, 9 Nov 2022 23:22:05 +0900 (JST)
Subject: [ruby-changes:74420] 80d145fa52 (master): Fix and add regexp tests
https://git.ruby-lang.org/ruby.git/commit/?id=80d145fa52 From 80d145fa52e9077dc7812a94f3fc41208fc58bd7 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune <make.just.on@g...> Date: Mon, 7 Nov 2022 09:03:12 +0900 Subject: Fix and add regexp tests --- test/ruby/test_regexp.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 3d3cdbb46e..5daf67a436 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -1589,7 +1589,7 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L1589 t = Time.now assert_raise_with_message(Regexp::TimeoutError, "regexp match timeout") do # A typical ReDoS case - /^(a*)*$/ =~ "a" * 1000000 + "x" + /^(a*)*\1$/ =~ "a" * 1000000 + "x" end t = Time.now - t @@ -1631,7 +1631,7 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L1631 Regexp.timeout = global_timeout - re = Regexp.new("^a*b?a*$", timeout: per_instance_timeout) + re = Regexp.new("^(a*)\\1b?a*$", timeout: per_instance_timeout) assert_equal(per_instance_timeout, re.timeout) t = Time.now @@ -1673,4 +1673,24 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L1673 assert_raise(ArgumentError) { Regexp.new("foo", timeout: -1) } end; end + + def test_cache_optimization_exponential + assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") + begin; + timeout = EnvUtil.apply_timeout_scale(0.2) + Regexp.timeout = timeout + + assert_nil(/^(a*)*$/ =~ "a" * 1000000 + "x") + end; + end + + def test_cache_optimization_square + assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") + begin; + timeout = EnvUtil.apply_timeout_scale(0.2) + Regexp.timeout = timeout + + assert_nil(/^a*b?a*$/ =~ "a" * 1000000 + "x") + end; + end end -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/