Skip to content

Commit 639b0b4

Browse files
jkeenanbingos
authored andcommitted
maybe_command(): account for undefined or empty argument
And thereby avoid uninitialized value warning. As demonstrated in cpan-testers/CPAN-Reporter#59 (comment), it is possible to exercise subroutine maybe_command() without a defined argument. We should preclude this from happening. Test explicitly for return value in edge cases as well as absence of warnings. Simplify code per Ilmari review: https://github.com/Perl-Toolchain-Gang/ExtUtils-MakeMaker/pull/449/files
1 parent 2216948 commit 639b0b4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/ExtUtils/MM_Unix.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,8 @@ Returns true, if the argument is likely to be a command.
28902890

28912891
sub maybe_command {
28922892
my($self,$file) = @_;
2893+
# $file = '' if (!defined $file or !length $file);
2894+
return unless defined $file and length $file;
28932895
return $file if -x $file && ! -d $file;
28942896
return;
28952897
}

t/MM_Unix.t

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BEGIN {
1212
plan skip_all => 'Non-Unix platform';
1313
}
1414
else {
15-
plan tests => 114;
15+
plan tests => 118;
1616
}
1717
}
1818

@@ -178,7 +178,31 @@ is ($t->libscan('Fatty'), 'Fatty', 'libscan on something not a VC file' );
178178

179179
open(FILE, ">command"); print FILE "foo"; close FILE;
180180
SKIP: {
181-
skip("no separate execute mode on VOS", 2) if $^O eq "vos";
181+
skip("no separate execute mode on VOS", 4) if $^O eq "vos";
182+
183+
{
184+
local $@;
185+
my $rv;
186+
my @warnings = ();
187+
local $SIG{__WARN__} = sub { push @warnings, shift; };
188+
eval { $rv = $t->maybe_command( undef ); };
189+
ok (! @warnings, "maybe_command emits no warnings with undefined argument");
190+
ok (! defined $rv,
191+
"maybe_command returns undef if not provided defined argument"
192+
);
193+
}
194+
195+
{
196+
local $@;
197+
my $rv;
198+
my @warnings = ();
199+
local $SIG{__WARN__} = sub { push @warnings, shift; };
200+
eval { $rv = $t->maybe_command( '' ); };
201+
ok (! @warnings, "maybe_command emits no warnings with empty-string argument");
202+
ok (! defined $rv,
203+
"maybe_command returns undef if not provided positive-length argument"
204+
);
205+
}
182206

183207
ok !$t->maybe_command('command') ,"non executable file isn't a command";
184208

0 commit comments

Comments
 (0)