This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove small memory leak in newATTRSUB that manifested as a
[perl5.git] / av.c
diff --git a/av.c b/av.c
index acc9963..d37ba01 100644 (file)
--- a/av.c
+++ b/av.c
@@ -1,6 +1,7 @@
 /*    av.c
  *
- *    Copyright (c) 1991-2002, Larry Wall
+ *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+ *    2000, 2001, 2002, 2003, by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -208,9 +209,11 @@ Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
             }
 
             sv = sv_newmortal();
-            mg_copy((SV*)av, sv, 0, key);
-            PL_av_fetch_sv = sv;
-            return &PL_av_fetch_sv;
+           sv_upgrade(sv, SVt_PVLV);
+           mg_copy((SV*)av, sv, 0, key);
+           LvTYPE(sv) = 't';
+           LvTARG(sv) = sv; /* fake (SV**) */
+           return &(LvTARG(sv));
         }
     }
 
@@ -453,8 +456,11 @@ Perl_av_clear(pTHX_ register AV *av)
        ary = AvARRAY(av);
        key = AvFILLp(av) + 1;
        while (key) {
-           SvREFCNT_dec(ary[--key]);
+           SV * sv = ary[--key];
+           /* undef the slot before freeing the value, because a
+            * destructor might try to modify this arrray */
            ary[key] = &PL_sv_undef;
+           SvREFCNT_dec(sv);
        }
     }
     if ((key = AvARRAY(av) - AvALLOC(av))) {