newSVrv() memory leak
Jan Dubois [Thu, 17 Aug 2000 18:31:55 +0000 (11:31 -0700)]
Message-ID: <l64ppsggaiuc4t6msm45iqohmhpjblb4qf@4ax.com>

p4raw-id: //depot/perl@6687

sv.c

diff --git a/sv.c b/sv.c
index 9e77330..f065084 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -5534,8 +5534,23 @@ Perl_newSVrv(pTHX_ SV *rv, const char *classname)
     SV_CHECK_THINKFIRST(rv);
     SvAMAGIC_off(rv);
 
+    if (SvTYPE(rv) >= SVt_PVMG) {
+       U32 refcnt = SvREFCNT(rv);
+       SvREFCNT(rv) = 0;
+       sv_clear(rv);
+       SvFLAGS(rv) = 0;
+       SvREFCNT(rv) = refcnt;
+    }
+
     if (SvTYPE(rv) < SVt_RV)
-      sv_upgrade(rv, SVt_RV);
+       sv_upgrade(rv, SVt_RV);
+    else if (SvTYPE(rv) > SVt_RV) {
+       (void)SvOOK_off(rv);
+       if (SvPVX(rv) && SvLEN(rv))
+           Safefree(SvPVX(rv));
+       SvCUR_set(rv, 0);
+       SvLEN_set(rv, 0);
+    }
 
     (void)SvOK_off(rv);
     SvRV(rv) = sv;