From 2ade40276be9f60ed06e7011b41a4c90f03e59b4 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 28 Mar 2022 15:03:17 +0900 Subject: [PATCH] re.c: raise Regexp::TimeoutError instead of RuntimeError --- re.c | 5 +++-- test/ruby/test_regexp.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/re.c b/re.c index b2ba6b2ef6..bcd13ddf77 100644 --- a/re.c +++ b/re.c @@ -27,7 +27,7 @@ #include "ruby/re.h" #include "ruby/util.h" -VALUE rb_eRegexpError; +VALUE rb_eRegexpError, rb_eRegexpTimeoutError; typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN]; #define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN) @@ -4153,7 +4153,7 @@ rb_reg_check_timeout(regex_t *reg, void *end_time_) else { if (*end_time < rb_hrtime_now()) { // timeout is exceeded - rb_raise(rb_eRuntimeError, "regexp match timeout"); + rb_raise(rb_eRegexpTimeoutError, "regexp match timeout"); } } } @@ -4304,6 +4304,7 @@ Init_Regexp(void) rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0); rb_define_method(rb_cRegexp, "timeout", rb_reg_timeout_get, 0); + rb_eRegexpTimeoutError = rb_define_class_under(rb_cRegexp, "TimeoutError", rb_eRegexpError); rb_define_singleton_method(rb_cRegexp, "timeout", rb_reg_s_timeout_get, 0); rb_define_singleton_method(rb_cRegexp, "timeout=", rb_reg_s_timeout_set, 1); diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 63572cf84a..fb989c74fa 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -1465,7 +1465,7 @@ class TestRegexp < Test::Unit::TestCase assert_equal(0.2, Regexp.timeout) t = Time.now - assert_raise_with_message(RuntimeError, "regexp match timeout") do + assert_raise_with_message(Regexp::TimeoutError, "regexp match timeout") do # A typical ReDoS case /^(a*)*$/ =~ "a" * 1000000 + "x" end @@ -1483,7 +1483,7 @@ class TestRegexp < Test::Unit::TestCase re = Regexp.new("^a*b?a*$", timeout: 0.2) t = Time.now - assert_raise_with_message(RuntimeError, "regexp match timeout") do + assert_raise_with_message(Regexp::TimeoutError, "regexp match timeout") do re =~ "a" * 1000000 + "x" end t = Time.now - t