292b33
From 316518b545904d368d703005f1622fde03349567 Mon Sep 17 00:00:00 2001
292b33
From: Father Chrysostomos <sprout@cpan.org>
292b33
Date: Fri, 21 Sep 2012 22:01:19 -0700
292b33
Subject: [PATCH] Free iterator when freeing tied hash
292b33
292b33
The current iterator was leaking when a tied hash was freed or
292b33
undefined.
292b33
292b33
Since we already have a mechanism, namely HvLAZYDEL, for freeing
292b33
HvEITER when not referenced elsewhere, we can use that.
292b33
292b33
Petr Pisar: Ported to 5.16.3.
292b33
---
292b33
 hv.c          |  3 +++
292b33
 t/op/svleak.t | 15 ++++++++++++++-
292b33
 2 files changed, 17 insertions(+), 1 deletion(-)
292b33
292b33
diff --git a/hv.c b/hv.c
292b33
index a031703..3c35341 100644
292b33
--- a/hv.c
292b33
+++ b/hv.c
292b33
@@ -2346,6 +2346,7 @@ Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
292b33
             if (entry) {
292b33
                 sv_setsv(key, HeSVKEY_force(entry));
292b33
                 SvREFCNT_dec(HeSVKEY(entry));       /* get rid of previous key */
292b33
+		HeSVKEY_set(entry, NULL);
292b33
             }
292b33
             else {
292b33
                 char *k;
292b33
@@ -2353,6 +2354,7 @@ Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
292b33
 
292b33
                 /* one HE per MAGICAL hash */
292b33
                 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
292b33
+		HvLAZYDEL_on(hv); /* make sure entry gets freed */
292b33
                 Zero(entry, 1, HE);
292b33
                 Newxz(k, HEK_BASESIZE + sizeof(const SV *), char);
292b33
                 hek = (HEK*)k;
292b33
@@ -2369,6 +2371,7 @@ Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
292b33
             Safefree(HeKEY_hek(entry));
292b33
             del_HE(entry);
292b33
             iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
292b33
+	    HvLAZYDEL_off(hv);
292b33
             return NULL;
292b33
         }
292b33
     }
292b33
diff --git a/t/op/svleak.t b/t/op/svleak.t
292b33
index 6cfee2e..2f09af3 100644
292b33
--- a/t/op/svleak.t
292b33
+++ b/t/op/svleak.t
292b33
@@ -13,7 +13,7 @@ BEGIN {
292b33
 	or skip_all("XS::APItest not available");
292b33
 }
292b33
 
292b33
-plan tests => 22;
292b33
+plan tests => 23;
292b33
 
292b33
 # run some code N times. If the number of SVs at the end of loop N is
292b33
 # greater than (N-1)*delta at the end of loop 1, we've got a leak
292b33
@@ -163,3 +163,16 @@ leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
292b33
 
292b33
 # [perl #114764] Attributes leak scalars
292b33
 leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');
292b33
+
292b33
+# Tied hash iteration was leaking if the hash was freed before itera-
292b33
+# tion was over.
292b33
+package t {
292b33
+    sub TIEHASH { bless [] }
292b33
+    sub FIRSTKEY { 0 }
292b33
+}
292b33
+leak(2, 0, sub {
292b33
+    my $h = {};
292b33
+    tie %$h, t;
292b33
+    each %$h;
292b33
+    undef $h;
292b33
+}, 'tied hash iteration does not leak');
292b33
-- 
292b33
1.8.1.4
292b33