Project

General

Profile

Actions

Bug #6366

closed

Regex freezes Ruby

Added by vertis (Luke Chadwick) about 10 years ago. Updated about 10 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
ruby -v:
ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.3.0]
[ruby-core:44647]

Description

I stumbled across a weird error on Ruby 1.8.7p352, where String.match locks up:

"#^(?:export)?\s*(\w+)=(.)".match(/^(?:export[\s]+)?([\w_])+?=(.*)/)

I had, apparently, pasted a regular expression into a file(bash) that I was testing, and commented it out. I'm assuming I did this to clear my clipboard. I was then running the following match on each line in the file & consistently had the ruby interpreter lockup (not even Ctrl-C was working, though Ctrl-Z did).

I have replicated this on both MacOS (homebrew) and CentOS 6 1.8.7p352, but this doesn't happen in Ruby 1.9.2p0.

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

  • Status changed from Open to Rejected

=begin
It's due to the bad regexp.

You should not repeat repetitive operators (({"([\w_]*)+?"})), which causes combinatorial explosion.

Try with (({"([\w_]+?)"})).
=end

Updated by vertis (Luke Chadwick) about 10 years ago

Sure, that works. You're absolutely correct about my regex error.

However, I still think it's a bug. The behaviour should be to fail with an Exception not lockup indefinitely.

Updated by shyouhei (Shyouhei Urabe) about 10 years ago

No, sorry. You cant detect whether your program locks up.

http://en.wikipedia.org/wiki/Halting_problem

Updated by vertis (Luke Chadwick) about 10 years ago

Right, but you could detect bad patterns in the regex like the double repetition. How does Ruby 1.9 NOT freeze?

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

Oniguruma, the regex engine in 1.9, optimizes it in its parser.
But 1.8 doesn't, and has no future.

Actions

Also available in: Atom PDF