* Florian G. Pflug (fgp / phlo.org) wrote:

> I noticed that there a quite a few ruby c-extensions that use blocking
> read, write and select calls without properly wrapping them with some
> rb_thread_select loop.
>
> This is annoying, because I leads to all ruby threads being blocked
> until the read, write or select finishes.

A similar problem can be seen with the built in Regexp module; find a
regexp which hangs the engine, and just *try* to make it time out.
timeout.rb doesn't work, because; you guessed it - it uses threads,
which Regexp.match blocks.

The closest I can get is a quick C wrapper to alarm(2) and:

  begin
    Alarm.set(2)
    trap('SIGALRM') { Process.kill("SIGINT", 0) }
    /#{str}/im.match(title)
  rescue Interrupt
    raise Damnit_I_Want_To_Use_Timeout_rb
  end

Which makes me somewhat nervous -- if I'm going to be letting users
specify their own regexp's, I don't want to use a hack to make sure they
can't make it hang by entering something evil.

In the mean time, does anyone have any hints for filtering a user
specified regexp so it's still mostly functional, but doesn't provide
opertunities for hanging the interpreter or so?  Maybe just removing
backtracking?

-- 
Thomas 'Freaky' Hurst  -  freaky / aagh.net  -  http://www.aagh.net/