Validation of user provided regex

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, Jul 12, 2007.

  1. In an app where a user may provide a Perl regular expression I want to
    validate the regex before it's applied. Currently I'm doing something like:

    {
    local $SIG{__WARN__} = sub { die $_[0] };
    eval { $regex = qr($regex) };
    die $@ if $@;
    }

    In other words I rely on Perl's ability to catch errors, where also
    warnings are treated as errors.

    Are there other appropriate checks that could be done?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 12, 2007
    #1
    1. Advertising

  2. Gunnar Hjalmarsson

    -berlin.de Guest

    Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    > In an app where a user may provide a Perl regular expression I want to
    > validate the regex before it's applied. Currently I'm doing something like:
    >
    > {
    > local $SIG{__WARN__} = sub { die $_[0] };
    > eval { $regex = qr($regex) };
    > die $@ if $@;
    > }
    >
    > In other words I rely on Perl's ability to catch errors, where also
    > warnings are treated as errors.
    >
    > Are there other appropriate checks that could be done?


    If code interpolations are a possibility, you may want to "use re
    'eval';" in the block. Or not, depending...

    Anno
     
    -berlin.de, Jul 12, 2007
    #2
    1. Advertising

  3. -berlin.de wrote:
    > Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    >> In an app where a user may provide a Perl regular expression I want to
    >> validate the regex before it's applied. Currently I'm doing something like:
    >>
    >> {
    >> local $SIG{__WARN__} = sub { die $_[0] };
    >> eval { $regex = qr($regex) };
    >> die $@ if $@;
    >> }
    >>
    >> In other words I rely on Perl's ability to catch errors, where also
    >> warnings are treated as errors.
    >>
    >> Are there other appropriate checks that could be done?

    >
    > If code interpolations are a possibility, you may want to "use re
    > 'eval';" in the block. Or not, depending...


    Thanks for the tip, but I really don't see a need for using (?{ ... }),
    so I guess I'd better not.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS News \(MS ILM\)
    Replies:
    0
    Views:
    419
    MS News \(MS ILM\)
    Aug 26, 2003
  2. Peter Hardy

    Ensuring users have provided valid html

    Peter Hardy, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    334
    Peter Hardy
    Dec 29, 2004
  3. Adi

    User Provided CSS

    Adi, Sep 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    328
    Just Me
    Sep 21, 2007
  4. Replies:
    3
    Views:
    807
    Reedick, Andrew
    Jul 1, 2008
  5. Nikos Chantziaras
    Replies:
    6
    Views:
    471
    Alf P. Steinbach /Usenet
    May 25, 2011
Loading...

Share This Page