Skip to content

Commit

Permalink
prepare_export_lexical: save PL_comppad safely
Browse files Browse the repository at this point in the history
When the pad being saved and the pad for PL_compcv is the same,
in some cases the actual exports would result in reallocating the
AvARRAY() for the saved PL_comppad.

The LEAVE in finish_export_lexical() would restore the old PL_comppad
(which is fine) and the pre-reallocation PL_curpad (which isn't fine).

This would later panic.

SAVECOMPPAD; restores PL_comppad on LEAVE and then restores PL_curpad
from PL_comppad, preventing the desync between those values.

It's unclear to me why only the save_BEGINs; causes this, but the fix
does fix a real problem and prevents the panics that I'm aware of
here.

Fixes Perl#21981
  • Loading branch information
tonycoz committed Mar 21, 2024
1 parent 743293b commit b245f7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ Perl_prepare_export_lexical(pTHX)
/* We need to have PL_comppad / PL_curpad set correctly for lexical importing */
ENTER;
SAVESPTR(PL_comppad_name); PL_comppad_name = PadlistNAMES(CvPADLIST(PL_compcv));
SAVESPTR(PL_comppad); PL_comppad = PadlistARRAY(CvPADLIST(PL_compcv))[1];
SAVESPTR(PL_curpad); PL_curpad = PadARRAY(PL_comppad);
SAVECOMPPAD();
PL_comppad = PadlistARRAY(CvPADLIST(PL_compcv))[1];
PL_curpad = PadARRAY(PL_comppad);
}

#define export_lexical(name, sv) S_export_lexical(aTHX_ name, sv)
Expand Down
9 changes: 9 additions & 0 deletions lib/builtin.t
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ TODO: {
}
}

# github #21981
{
fresh_perl_is(<<'EOS', "", {}, "github 21981: panic in intro_my");
use B;
BEGIN { B::save_BEGINs; }
use v5.39;
EOS
}

# vim: tabstop=4 shiftwidth=4 expandtab autoindent softtabstop=4

done_testing();

0 comments on commit b245f7c

Please sign in to comment.