in reply to Re: Tainting problem on Strawberry perl
in thread Tainting problem on Strawberry perl

Just to clarify this isn't my code, this is the code from Template::Provider, I'm trying to get my Catalyst stuff to work on IIS.

Onward, your suggestion is sound and $wdir = File::Spec->catfile($cdir, "$1"); even works, but the question remains, how is it broken? What makes my Strawberry 5.10 different from most other peoples is that I keep the components in it fairly up to date. Which component could have broken this? File::Spec maybe? and should this bug be fixed in Tempalte::Provider or elsewhere (which is what I lean towards).


Evan Carroll
www.EvanCarroll.com

Replies are listed 'Best First'.
Re^3: Tainting problem on Strawberry perl
by ikegami (Patriarch) on Feb 28, 2008 at 23:04 UTC

    I don't know why, sorry, but I can confirm it's probably a bug in your build. It doesn't affect my ActivePerl builds. I simplified your code to

    use File::Spec::Win32; my $wdir = 'C:/strawberry'; $wdir =~ s[:][]g; $wdir =~ /(.*)/; print File::Spec::Win32->catfile('C:/cache', $1), "\n";

    and I get (ActivePerl 5.8.8 & 5.10.0)

    C:\cache\C\strawberry

    You presumably get

    C:\cache\C:
      Wow you found it, in that you made a test, I guess it has nothing to do with a being tainted, I'll rename node. That is correct with your code, I get what you've assumed I would get.


      Evan Carroll
      www.EvanCarroll.com

        Some more (speculation) on the subject:

        • $wdir =~ s[:][]g; can probably be removed from the test. I didn't want to remove too much, but I don't see how this could possibly trigger the bug. So really, the minimal test is probably

          use File::Spec::Win32; my $wdir = 'C/strawberry'; $wdir =~ /(.*)/; print File::Spec::Win32->catfile('C:/cache', $1), "\n";
        • The "C:" being appended probably comes from $cdir. The last regexp to successfully match in catfile is $path =~ s/^([a-z]:)/\u$1/s; (via canonpath via catdir), where path is a copy of $cdir.

          That would explain why $1 has been changed but not how it managed to get appended. I wouldn't mind having a look at your File/Spec/Win32.pm, specifically catfile.

Re^3: Tainting problem on Strawberry perl
by syphilis (Archbishop) on Feb 28, 2008 at 23:42 UTC
    Which component could have broken this? File::Spec maybe?

    Looks likely. Using the test case that ikegami provided, I find that I get his (correct) output with File::Spec::Win32-3.2501. But when I update to PathTools-3.2701, I get your (broken) output.

    Cheers,
    Rob

      Bingo! It is as I suspected in my initial post. I was confused in my latter posts because I was looking at the wrong version.

      Changes to $1 results in changes to $_[1], since $_[1] is aliased to $1. I was looking at the version in perl-5.10.0, not the one from PathTools-3.2701. The one in perl-5.10.0 copies @_ into local variables, so it works fine. The one in Pathtools-3.2701 works with @_ directly, so it picks up the change to $1.

      That technically makes the bug PathTools's, since it should localize the globlals it modifies (like $1). However, it's rarely done (especially for $1, $! and $@) and it's actually rather hard to do when working with @_ directly and package variables (like $1), so it's best not to pass globals to functions and to follow the tips I already posted.

      Update: Here's some code the illustrate the issue.

      sub f_bad { 'b' =~ /(.*)/; return $_[0]; } sub f_good1 { my ($var) = @_; 'b' =~ /(.*)/; return $var; } sub f_good2 { { local $1; 'b' =~ /(.*)/; } return $_[0]; } 'a' =~ /(.*)/; print f_bad($1), "\n"; # b 'a' =~ /(.*)/; print f_good1($1), "\n"; # a 'a' =~ /(.*)/; print f_good2($1), "\n"; # a

      Unforunately, sub { local $1; 'b' =~ /(.*)/; return $_[0]; } doesn't work since local doesn't create a new variable.