Skip to content

Commit

Permalink
Avoid mangling /bin non-perl shebangs on merged-/usr systems
Browse files Browse the repository at this point in the history
If the shebang is absolute and exists in PATH, but was not the first
one found, leave it alone if it's actually the same file as first one.

This avoids packages built on merged-/usr systems with /usr/bin before
/bin in the path breaking when installed on systems without merged
/usr.  See e.g. https://bugs.debian.org/913637.
  • Loading branch information
ilmari authored and bingos committed Nov 15, 2018
1 parent 56817fa commit 9766f9c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
22 changes: 20 additions & 2 deletions lib/ExtUtils/MM_Unix.pm
Expand Up @@ -1256,8 +1256,8 @@ sub _fixin_replace_shebang {
my ( $self, $file, $line ) = @_;

# Now figure out the interpreter name.
my ( $cmd, $arg ) = split ' ', $line, 2;
$cmd =~ s!^.*/!!;
my ( $origcmd, $arg ) = split ' ', $line, 2;
(my $cmd = $origcmd) =~ s!^.*/!!;

# Now look (in reverse) for interpreter in absolute PATH (unless perl).
my $interpreter;
Expand Down Expand Up @@ -1286,6 +1286,24 @@ sub _fixin_replace_shebang {
$interpreter = $maybefile;
}
}

# If the shebang is absolute and exists in PATH, but was not
# the first one found, leave it alone if it's actually the
# same file as first one. This avoids packages built on
# merged-/usr systems with /usr/bin before /bin in the path
# breaking when installed on systems without merged /usr
if ($origcmd ne $interpreter and $self->file_name_is_absolute($origcmd)) {
my $origdir = dirname($origcmd);
if ($self->maybe_command($origcmd) && grep { $_ eq $origdir } @absdirs) {
my ($odev, $oino) = stat $origcmd;
my ($idev, $iino) = stat $interpreter;
if ($odev == $idev && $oino == $iino) {
warn "$origcmd is the same as $interpreter, leaving alone"
if $Verbose;
$interpreter = $origcmd;
}
}
}
}

# Figure out how to invoke interpreter on this machine.
Expand Down
33 changes: 32 additions & 1 deletion t/fixin.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {

use File::Spec;

use Test::More tests => 22;
use Test::More tests => 30;

use Config;
use TieOut;
Expand Down Expand Up @@ -123,3 +123,34 @@ END
}
);
}

SKIP: {
eval { chmod(0755, "usrbin/interp") }
or skip "no chmod", 8;

my $dir = getcwd();
local $ENV{PATH} = join $Config{path_sep}, map "$dir/$_", qw(usrbin bin);

test_fixin(<<END,
#!$dir/bin/interp
blah blah blah
END
sub {
is $_[0], "#!$dir/usrbin/interp\n", 'interpreter updated to one found in PATH';
}
);

eval { symlink("../usrbin/interp", "bin/interp") }
or skip "no symlinks", 4;

test_fixin(<<END,
#!$dir/bin/interp
blah blah blah
END
sub {
is $_[0], "#!$dir/bin/interp\n", 'symlinked interpreter later in PATH not mangled';
}
);
}
3 changes: 3 additions & 0 deletions t/lib/MakeMaker/Test/Setup/BFD.pm
Expand Up @@ -53,6 +53,9 @@ program - this is a program
=cut
1;
END
'Big-Dummy/usrbin/interp' => <<'END',
This is a dummy interpreter
END

'Big-Dummy/test.pl' => <<'END',
Expand Down

0 comments on commit 9766f9c

Please sign in to comment.