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 #21981
  • Loading branch information
tonycoz committed Mar 24, 2024
1 parent 4ec23de commit a2b66c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions builtin.c
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
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 a2b66c2

Please sign in to comment.