A co-worker recently added some modules to a large perl program that
used $&, $' and $` (a.k.a. the "naughty match variables"). I know these
add a large performance penalty for all regular expressions in the
program, so I removed all the uses. Then I tried all three of the
methods in
Mastering Regular Expressions, second
edition, p. 358, "How to Check Whether Your Code is Tainted by $&".
Only the last method on that page works for perl 5.8.0. The
"-Mre=debug" does not show either 'Enabling $`, $&, $' support' or
'Omitting $`, $&, $' support' any more.
The Devel::SawAmpersand doesn't work either. It gives false positives
on trivial programs that don't have the "naughty match variables".
Here is the subroutine that actually worked:
use strict;
use Time::HiRes;
sub CheckNaughtiness ()
{
my $text = 'x' x 10_000;
my $start = Time::HiRes::time();
for (my $i = 0; $i < 5_000; $i++) { }
my $overhead = Time::HiRes::time() - $start;
$start = Time::HiRes::time();
for (my $i = 0; $i < 5_000; $i++) { $text =~ m/^/}
my $delta = Time::HiRes::time() - $start;
printf "It seems your code is %s (overhead=%.2f, delta=%.2f)\n",
($delta > $overhead*5) ? "naughty" : "clean", $overhead, $delta;
}
To my great surprise, when I traced it out I found two CPAN modules (so
far) that we use are also tainted in this way:
Printer and
Math::MatrixReal. I have sent mail to the maintainers of these
modules pointing out the issue.
It makes me wonder how many other CPAN modules are tainted with "naughty
match variables". Another way to get tainted is to do:
# Don't do this:
use English;
# Do this instead:
use English qw( -no_match_vars );
Has anyone else noticed this problem? Should there be a general check for
"naughty match variables" for code submitted to CPAN?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.