Skip to content

class: don't leak the default initialiser ops if they're forbidden #21457

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
Sep 14, 2023
Merged
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
23 changes: 19 additions & 4 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ Perl_class_seal_stash(pTHX_ HV *stash)
assert(HvSTASH_IS_CLASS(stash));
struct xpvhv_aux *aux = HvAUX(stash);

/* generate initfields CV */
{
if (PL_parser->error_count == 0) {
/* generate initfields CV */
I32 floor_ix = PL_savestack_ix;
SAVEI32(PL_subline);
save_item(PL_subname);
Expand Down Expand Up @@ -794,6 +794,18 @@ Perl_class_seal_stash(pTHX_ HV *stash)

aux->xhv_class_initfields_cv = initfields;
}
else {
/* we had errors, clean up and don't populate initfields */
PADNAMELIST *fieldnames = aux->xhv_class_fields;
if (fieldnames) {
for(SSize_t i = PadnamelistMAX(fieldnames); i >= 0 ; i--) {
PADNAME *pn = PadnamelistARRAY(fieldnames)[i];
OP *valop = PadnameFIELDINFO(pn)->defop;
if (valop)
op_free(valop);
}
}
}
}

void
Expand Down Expand Up @@ -1010,11 +1022,14 @@ Perl_class_set_field_defop(pTHX_ PADNAME *pn, OPCODE defmode, OP *defop)

assert(HvSTASH_IS_CLASS(PL_curstash));

forbid_outofblock_ops(defop, "field initialiser expression");

if(PadnameFIELDINFO(pn)->defop)
op_free(PadnameFIELDINFO(pn)->defop);

/* set here to ensure clean up if forbid_outofblock_ops() throws */
PadnameFIELDINFO(pn)->defop = defop;

forbid_outofblock_ops(defop, "field initialiser expression");

char sigil = PadnamePV(pn)[0];
switch(sigil) {
case '$':
Expand Down