re.c: raise Regexp::TimeoutError instead of RuntimeError
This commit is contained in:
parent
34b288f8d4
commit
2ade40276b
Notes:
git
2022-03-30 16:51:09 +09:00
5
re.c
5
re.c
@ -27,7 +27,7 @@
|
|||||||
#include "ruby/re.h"
|
#include "ruby/re.h"
|
||||||
#include "ruby/util.h"
|
#include "ruby/util.h"
|
||||||
|
|
||||||
VALUE rb_eRegexpError;
|
VALUE rb_eRegexpError, rb_eRegexpTimeoutError;
|
||||||
|
|
||||||
typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
|
typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
|
||||||
#define errcpy(err, msg) strlcpy((err), (msg), 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 {
|
else {
|
||||||
if (*end_time < rb_hrtime_now()) {
|
if (*end_time < rb_hrtime_now()) {
|
||||||
// timeout is exceeded
|
// 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, "named_captures", rb_reg_named_captures, 0);
|
||||||
rb_define_method(rb_cRegexp, "timeout", rb_reg_timeout_get, 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_get, 0);
|
||||||
rb_define_singleton_method(rb_cRegexp, "timeout=", rb_reg_s_timeout_set, 1);
|
rb_define_singleton_method(rb_cRegexp, "timeout=", rb_reg_s_timeout_set, 1);
|
||||||
|
|
||||||
|
@ -1465,7 +1465,7 @@ class TestRegexp < Test::Unit::TestCase
|
|||||||
assert_equal(0.2, Regexp.timeout)
|
assert_equal(0.2, Regexp.timeout)
|
||||||
|
|
||||||
t = Time.now
|
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 typical ReDoS case
|
||||||
/^(a*)*$/ =~ "a" * 1000000 + "x"
|
/^(a*)*$/ =~ "a" * 1000000 + "x"
|
||||||
end
|
end
|
||||||
@ -1483,7 +1483,7 @@ class TestRegexp < Test::Unit::TestCase
|
|||||||
re = Regexp.new("^a*b?a*$", timeout: 0.2)
|
re = Regexp.new("^a*b?a*$", timeout: 0.2)
|
||||||
|
|
||||||
t = Time.now
|
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"
|
re =~ "a" * 1000000 + "x"
|
||||||
end
|
end
|
||||||
t = Time.now - t
|
t = Time.now - t
|
||||||
|
Loading…
x
Reference in New Issue
Block a user