perlsec tells us the following:

The exception to the principle of "one tainted value taints the whole expression" is with the ternary conditional operator "?:". Since code with a ternary conditional $result = $tainted_value ? "Untainted" : "Also untainted"; is effectively if ( $tainted_value ) { $result = "Untainted"; } else { $result = "Also untainted"; } it doesn't make sense for $result to be tainted.

However, this does not appear to be the whole story because those two constructions are not in fact identical as the following script demonstrates

#!/usr/bin/perl -T use warnings; use strict; use Scalar::Util qw/tainted/; # Note: $t is tainted my $t = "purple" . substr($^X,0,0); my $foo = "blah"; my $bar = "foo: $foo"; my $one; $one = $t ? "the $foo" : $bar; print "1. tainted\n" if tainted($one); my $two; $two = $t ? "the foo" : $bar; print "2. tainted\n" if tainted($two); my $three; if ($t) { $three = "the $foo"; } else { $three = $bar; } print "3. tainted\n" if tainted($three); my $four; if ($t) { $four = "the foo"; } else { $four = $bar; } print "4. tainted\n" if tainted($four); print "5. tainted\n" if tainted($bar); print "6. tainted\n" if tainted("the $foo"); my $seven = ""; $seven = "the $foo" if $t; print "7. tainted\n" if tainted($seven);

The above script prints

1. tainted 7. tainted

in perl 5.8.8 and perl 5.10.0.

For one I find edge cases like this rather interesting. But I also I wonder is whether this should be filed as a bug and whether it should be filed as a perl bug or a documentation bug.

Update: Point out that $t is tainted.
Update: Add tests 5, 6, and 7.
Update: Filed bug 59916

Good Day,
    Dean


In reply to "one tainted value taints the whole expression" by duelafn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.