Skip to content

check the IO object exists when writing to IO magic variables #20736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions mg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3072,25 +3072,55 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
break;
case '^':
Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
{
IO * const io = GvIO(PL_defoutgv);
if (!io)
break;

Safefree(IoTOP_NAME(io));
IoTOP_NAME(io) = savesvpv(sv);
IoTOP_GV(io) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
}
break;
case '~':
Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
{
IO * const io = GvIO(PL_defoutgv);
if (!io)
break;

Safefree(IoFMT_NAME(io));
IoFMT_NAME(io) = savesvpv(sv);
IoFMT_GV(io) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
}
break;
case '=':
IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
{
IO * const io = GvIO(PL_defoutgv);
if (!io)
break;

IoPAGE_LEN(io) = (SvIV(sv));
}
break;
case '-':
IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
{
IO * const io = GvIO(PL_defoutgv);
if (!io)
break;

IoLINES_LEFT(io) = (SvIV(sv));
if (IoLINES_LEFT(io) < 0L)
IoLINES_LEFT(io) = 0L;
}
break;
case '%':
IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
{
IO * const io = GvIO(PL_defoutgv);
if (!io)
break;

IoPAGE(io) = (SvIV(sv));
}
break;
case '|':
{
Expand Down
16 changes: 15 additions & 1 deletion t/io/defout.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN {

$|=0; # test.pl makes it 1, and that conflicts with the below.

plan tests => 16;
plan tests => 22;


my $stdout = *STDOUT;
Expand Down Expand Up @@ -48,6 +48,20 @@ $- = 1; pass '$- = 1';
$% = 1; pass '$% = 1';
$| = 1; pass '$| = 1';

# test a NULLed GV
my $t = tempfile;
open FOO, ">", $t or die;
select(FOO);
my $io = *FOO{IO};
*FOO = 0;
$^ = 1; pass 'empty GV: $^ = 1';
$~ = 1; pass 'empty GV: $~ = 1';
$= = 1; pass 'empty GV: $= = 1';
$- = 1; pass 'empty GV: $- = 1';
$% = 1; pass 'empty GV: $% = 1';
$| = 1; pass 'empty GV: $| = 1';
close $io;

# Switch to STDERR for this test, so we do not lose our test output
my $stderr = *STDERR;
select($stderr);
Expand Down