Skip to content

Commit

Permalink
Fix and add regexp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and mame committed Nov 9, 2022
1 parent ff2998a commit 80d145f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions test/ruby/test_regexp.rb
Expand Up @@ -1589,7 +1589,7 @@ def test_s_timeout
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
Expand Down Expand Up @@ -1631,7 +1631,7 @@ def per_instance_redos_test(global_timeout, per_instance_timeout, expected_timeo
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
Expand Down Expand Up @@ -1673,4 +1673,24 @@ def test_timeout_corner_cases
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

0 comments on commit 80d145f

Please sign in to comment.