diff options
author | Yusuke Endoh <mame@ruby-lang.org> | 2022-10-24 18:03:26 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-24 18:03:26 +0900 |
commit | b51b22513f6a2a514a9e614e7a9f6e0df6c0c985 (patch) | |
tree | 00d099e3804c63e9957b7c2c9c0652723a7efcd6 /re.c | |
parent | 6700fa7f62b040b5f69c3c5c3f5dbe740910c990 (diff) | |
download | ruby-b51b22513f6a2a514a9e614e7a9f6e0df6c0c985.tar.gz |
Fix per-instance Regexp timeout (#6621)
Fix per-instance Regexp timeout
This makes it follow what was decided in [Bug #19055]:
* `Regexp.new(str, timeout: nil)` should respect the global timeout
* `Regexp.new(str, timeout: huge_val)` should use the maximum value that
can be represented in the internal representation
* `Regexp.new(str, timeout: 0 or negative value)` should raise an error
Diffstat (limited to 're.c')
-rw-r--r-- | re.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -3772,6 +3772,8 @@ str_to_option(VALUE str) * If optional keyword argument +timeout+ is given, * its float value overrides the timeout interval for the class, * Regexp.timeout. + * If +nil+ is passed as +timeout, it uses the timeout interval + * for the class, Regexp.timeout. * * With argument +regexp+ given, returns a new regexp. The source, * options, timeout are the same as +regexp+. +options+ and +n_flag+ @@ -3847,7 +3849,9 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self) { double limit = NIL_P(timeout) ? 0.0 : NUM2DBL(timeout); - if (limit < 0) limit = 0; + if (!NIL_P(timeout) && limit <= 0) { + rb_raise(rb_eArgError, "invalid timeout: %"PRIsVALUE, timeout); + } double2hrtime(®->timelimit, limit); } @@ -4478,7 +4482,9 @@ rb_reg_s_timeout_set(VALUE dummy, VALUE limit) rb_ractor_ensure_main_ractor("can not access Regexp.timeout from non-main Ractors"); - if (timeout < 0) timeout = 0; + if (!NIL_P(limit) && timeout <= 0) { + rb_raise(rb_eArgError, "invalid timeout: %"PRIsVALUE, limit); + } double2hrtime(&rb_reg_match_time_limit, timeout); return limit; |