-#!./perl
+#!./perl -T
BEGIN {
chdir 't' if -d 't';
SKIP: {
skip "No native pwd command found to test against", 4 unless $pwd_cmd;
- chomp(my $start = `$pwd_cmd`);
+ local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
+ my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
+ chomp(my $start = `$pwd_cmd_untainted`);
+
# Win32's cd returns native C:\ style
$start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
# DCL SHOW DEFAULT has leading spaces
my $cwd = getcwd();
require File::Spec;
my $path = @_ ? shift : File::Spec->curdir;
- CORE::chdir($path) || croak "Cannot chdir to $path:$!";
+ CORE::chdir($path) || croak "Cannot chdir to $path: $!";
my $realpath = getcwd();
- CORE::chdir($cwd) || croak "Cannot chdir back to $cwd:$!";
+ # I cannot think of an untainting regular expression
+ # that wouldn't also (a) be unportable (b) disqualify valid pathnames
+ # so just untainting all of it here and relying on -d and CORE::chdir
+ # to verify the validity.
+ # --jhi
+ my ($cwd_untainted) = ($cwd =~ /^(.+)$/);
+ -d $cwd_untainted && CORE::chdir($cwd_untainted) ||
+ croak "Cannot chdir back to $cwd: $!";
$realpath;
}