Project

General

Profile

Actions

Bug #22063

closed

Regexp timeout accepts NaN and disables timeout

Bug #22063: Regexp timeout accepts NaN and disables timeout

Added by afurm (Andrii Furmanets) 1 day ago. Updated 1 day ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-05-11T07:56:09Z master d0ea61cb87) +PRISM [arm64-darwin25]
[ruby-core:125456]

Description

Regexp.timeout= and Regexp.new(..., timeout:) currently accept Float::NAN.
The value passes the positive-timeout validation because NaN <= 0 is false, and
then double2hrtime stores it as a zero timeout value.

This makes both APIs behave as if no timeout was configured:

p Regexp.new("foo", timeout: Float::NAN).timeout
#=> nil

Regexp.timeout = Float::NAN
p Regexp.timeout
#=> nil

Float::NAN is not a valid positive timeout value, so I expect it to raise
ArgumentError, the same as 0 and negative values.

Reproduced on master:

ruby 4.1.0dev (2026-05-11T07:56:09Z master d0ea61cb87) +PRISM [arm64-darwin25]

Proposed fix:

Reject NaN in the shared regexp timeout validation helper before converting the
double to rb_hrtime_t.

Pull request: https://github.com/ruby/ruby/pull/16917

Actions

Also available in: PDF Atom