Skip to content

Commit

Permalink
Fix "perl5i -e" from segfaulting.
Browse files Browse the repository at this point in the history
It would walk off argv and then try to printf garbage (probably null).

For #269
  • Loading branch information
schwern committed Feb 7, 2015
1 parent b98b956 commit d096ee5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bin/perl5i.c.PL
Expand Up @@ -39,7 +39,7 @@ int main (int argc, char* argv[]) {
const char* perl_cmd = "%s";
char* perl_args[argc+1];
char* dash_m = (char *)malloc(sizeof(char) * (strlen(argv[0]) + 20));
char* program;
char* program = "";
int saw_dash_e = 0;
strcpy(dash_m, "-Mperl5i::cmd=");
Expand All @@ -56,6 +56,7 @@ int main (int argc, char* argv[]) {
&& dash_e > argv[i]
&& *(dash_e-1) == '-'
&& (strchr(argv[i], '-') == argv[i])
&& i+1 < argc
)
{
saw_dash_e = 1;
Expand All @@ -70,8 +71,8 @@ int main (int argc, char* argv[]) {
}
/* Skip the next argument, its the program */
program = argv[i+1];
i++;
program = argv[i];
continue;
}
Expand Down
17 changes: 17 additions & 0 deletions t/command_line_wrapper.t
Expand Up @@ -64,4 +64,21 @@ is capture {system @perl5icmd, '-e', q($fun="yay"; say $fun;)}, "yay\n", 'no str
is capture {system ($^X, '-Ilib', '-Mperl5i::latest', '-e', q|$fun="yay"; say $fun;|)},
"yay\n", q{no strict vars for perl -Mperl5i::latest -e '...'};

# It acts like Perl when given weird arguments.
{
# We expect these system calls to return non-zero
no autodie "system";

my %tests = (
"-e with no code" => ["-e"],
"null byte" => ["\\000", "-e"],
);

%tests->each(func($test_name,$args) {
is capture {system @perl5icmd, $args},
capture {system $^X, $args},
$test_name;
});
}

done_testing;

0 comments on commit d096ee5

Please sign in to comment.