On Nov 15, Jennifer Garner said:

I can't understand for this script below,I want somebody here to give me some help.Thanks.

sub is_tainted{
   my $var=shift;
   my $blank=substr($var,0,0);
   return not eval {eval "1 || $blank" || 1};
}

That subroutine estimate for if some given var is tainted or not.But I can't know how it works.

If a variable is tainted, then any substring of that variable is also tainted. In addition, it is illegal to eval() any string that is tainted. The inclusion of a tainted string inside another string makes that whole string tainted.

Therefore: if $var is tainted, the $blank will also be tainted (even though it's a substring of zero characters). If $blank is then tainted, then the code

  eval { eval "1 || $blank" || 1 }

will return false (since the eval { ... } catches fatal errors, and the eval "1 || $blank" raises a fatal error because $blank is tainted), and therefore,

  return not eval { eval "1 || $blank" || 1 };

returns true, stating that $var is indeed tainted. If $var wasn't tainted, then

  eval "1 || $blank" || 1

returns 1, and

  return not eval { 1 }

returns false, stating that $var wasn't tainted.

*whew*

Frankly, I find the 'eval "1 || $blank" || 1' silly, since the whole reason the '... || 1' is needed is since $blank is a blank string and the code '1 || ' is invalid Perl. Long story short, I'd have written:

  sub is_tainted {
    return not eval { eval 1 . substr($_[0], 0, 0) };
  }

It's much more concise.  If $_[0] isn't tainted, then

  not eval { eval 1 . substr($_[0], 0, 0) }
  ->
  not eval { eval 1 }
  ->
  not eval { 1 }
  ->
  not 1
  ->
  false

whereas if $_[1] is tainted, then the eval { ... } returns false since a fatal error is raised because

  eval 1 . substr($_[0], 0, 0)

is illegal if $_[0] is tainted.

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to