Skip to content

Commit

Permalink
Prevent @argv and <> from conflicting.
Browse files Browse the repository at this point in the history
Issue Perl#19340: This prevents an obscure segfault.
  • Loading branch information
FGasper committed Feb 8, 2022
1 parent b0a34aa commit 979aa84
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion doio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1393,8 +1393,14 @@ Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
return NULL;
while (av_count(GvAV(gv)) > 0) {
STRLEN oldlen;

/* Leave this SV unfreed. Most of the time it will leak,
* but as the number of args is likely small that shouldn't
* matter much. More importantly, this avoids a double-free
* in the event that the SV ends up assigned to $_.
*/
SV *const sv = av_shift(GvAV(gv));
SAVEFREESV(sv);

SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
sv_setsv(GvSVn(gv),sv);
SvSETMAGIC(GvSV(gv));
Expand Down
13 changes: 13 additions & 0 deletions t/run/argv_free.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!./perl

BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
skip_all_without_config('d_fcntl');
}

system $^X, '-e', 'close STDIN; map <>, @ARGV', 1, 2;
is($?, 0, '@ARGV does not conflict with <>');

done_testing;

0 comments on commit 979aa84

Please sign in to comment.