Project

General

Profile

Actions

Bug #12103

closed

ruby process hangs while executing regular expression.

Bug #12103: ruby process hangs while executing regular expression.

Added by antonyr (Antony Raj) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15], jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 24.45-b08 on 1.7.0_45-b18 +jit [darwin-x86_64]
[ruby-core:73948]

Description

The following code hangs

regex = /(\((\w+([\p{Punct}\s]{,3}\w*)*)\))/i
detail = "(said companies being fictitous sellers"
detail =~ regex

However when I change {,3} to {1,3}, the code executes and moves on. Otherwise hangs

Updated by duerst (Martin Dürst) almost 10 years ago Actions #1 [ruby-core:74019]

  • Status changed from Open to Rejected

I simplified this as follows:
regex = /(a+( ?a*))/
detail = "(aaaa aaaaaaa aaaaa aaaaaaaaa aaaaa"
It still hangs. But it's easier to see why: The part (a+( ?a
)* allows many different ways to match the same thing, and all these are tried. For more background, please see http://www.regular-expressions.info/catastrophic.html.

Updated by normalperson (Eric Wong) almost 10 years ago Actions #2 [ruby-core:74030]

wrote:

Status changed from Open to Rejected

I simplified this as follows:
regex = /(a+( ?a*)*)/
detail = "(aaaa aaaaaaa aaaaa aaaaaaaaa aaaaa"

Keep in mind the equivalent Perl terminates just fine:

my $detail = "(aaaa aaaaaaa aaaaa aaaaaaaaa aaaaa";
$detail =~ /\(a+( ?a*)*\)/;

Tested on an old perl v5.14.2 from Debian wheezy (oldstable).

So I prefer we acknowledge cases like these as bugs to at least
encourage someone to fix them.

Not a high priority for me, though, as I'm not familiar with
the inner workings of RE engines (and also know to avoid tricky REs).

Updated by shyouhei (Shyouhei Urabe) almost 10 years ago Actions #3 [ruby-core:74039]

On 02/28/2016 12:22 PM, Eric Wong wrote:

Keep in mind the equivalent Perl terminates just fine:

Yes. But,

So I prefer we acknowledge cases like these as bugs to at least
encourage someone to fix them.

Ruby's (and Perl's) RE grammar is known to be NP complete. Fixing this ``issue'' ultimately requires a proof of P=NP. I don't think it'll happen in a near future.

I guess Perl's RE engine can happen to handle this specific /(a+( ?a*)*)/ case, maybe due to their optimization.

Actions

Also available in: PDF Atom