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 9bc9e83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -6166,6 +6166,7 @@ t/re/uniprops09.t Test unicode \p{} regex constructs
t/re/uniprops10.t Test unicode \p{} regex constructs
t/re/user_prop_race_thr.t Test races in user-defined \p{} under threads
t/README Instructions for regression tests
t/run/argv_free.t Ensure no conflicts between @ARGV and <>.
t/run/cloexec.t Test close-on-exec.
t/run/dtrace.pl For dtrace.t
t/run/dtrace.t Test for DTrace probes
Expand Down
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 9bc9e83

Please sign in to comment.