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.
####
#!/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);
####
1. tainted
7. tainted