Skip to content

Commit

Permalink
Fix leak when smartmatching against a coderef
Browse files Browse the repository at this point in the history
Previously, when smartmatching against a code reference, the return
value would be leaked. This fixes that leak.
  • Loading branch information
Leont committed Aug 31, 2023
1 parent c3ddef3 commit 88a35d6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
4 changes: 0 additions & 4 deletions pp_ctl.c
Expand Up @@ -5888,17 +5888,13 @@ S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
sm_any_sub:
DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
ENTER_with_name("smartmatch_coderef");
SAVETMPS;
PUSHMARK(SP);
PUSHs(d);
PUTBACK;
c = call_sv(e, G_SCALAR);
SPAGAIN;
if (c == 0)
PUSHs(&PL_sv_no);
else if (SvTEMP(TOPs))
SvREFCNT_inc_void(TOPs);
FREETMPS;
LEAVE_with_name("smartmatch_coderef");
RETURN;
}
Expand Down
5 changes: 4 additions & 1 deletion t/op/svleak.t
Expand Up @@ -15,7 +15,7 @@ BEGIN {

use Config;

plan tests => 153;
plan tests => 154;

# run some code N times. If the number of SVs at the end of loop N is
# greater than (N-1)*delta at the end of loop 1, we've got a leak
Expand Down Expand Up @@ -646,6 +646,9 @@ my $refcount;
PERL
}

my $one = 1;
leak 2, 0, sub { 1 ~~ sub { 1 + $one } }, 'Smartmatch doesn\'t leak';

# the initial implementation of the require hook had some leaks

sub hook::before { $_[0] = "NoSuchFile2" if $_[0] =~/ NoSuch/;
Expand Down

0 comments on commit 88a35d6

Please sign in to comment.