summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2011-08-18 08:50:02 -0700
committerFather Chrysostomos <sprout@cpan.org>2011-08-31 13:28:52 -0700
commit790db5db7e6fc8402a2044ebc8cace4cdf563f50 (patch)
tree2866a9565e2c8fd418cec49b2471fca1f4dffc57
parentfe2b14024013434281bd9a6383f9d4d84312b355 (diff)
downloadperl-790db5db7e6fc8402a2044ebc8cace4cdf563f50.tar.gz
[perl #97020] Carp (actually caller) leaking memory
Commit eff7e72c3 (Detect incomplete caller overrides in Carp) used this little trick for detecting a @DB::args that an overridden caller() failed to set: + @args = \$i; # A sentinal, which no-one else has the address of But there is a bug in caller(). The first time caller tries to write to @DB::args, it calls Perl_init_dbargs first. That function checks whether @DB::args is AvREAL, in case someone has assigned to it, and takes appropriate measures. But caller doesn’t bother calling Perl_init_dbargs more than once. So manually-assigned items in @DB::args would leak, starting with the *second* call to caller. Commit eff7e72c3 triggered that bug, resulting in a regression in Carp, in that it started leaking. eff7e72c3 was backported to 5.12.2 with commit 97705941a4, so in both 5.12 and 5.14 Carp is affected. This bug (the caller bug, not Carp’s triggering thereof) also affects any caller overrides that set @DB::args themselves, if there are alternate calls to the overridden caller and CORE::caller. This commit fixes that by changing the if (!PL_dbargs) condition in pp_caller to if (!PL_dbargs || AvREAL(PL_dbargs)). I.e., if @args is either uninitialised or AvREAL then call Perl_init_dbargs. Perl_init_dbargs also has a bug in it, that this fixes: The array not only needs AvREAL turned off, but also AvREIFY turned on, so that assignments to it that occur after its initialisation turn AvREAL back on again. (In fact, Larry Wall added a comment suggesting this back in perl 5.000.) (cherry-picked from af80dd863acea8450a9f41ae03645f4d69dad091)
-rw-r--r--av.h2
-rw-r--r--perl.c2
-rw-r--r--pp_ctl.c2
-rw-r--r--t/op/caller.t13
4 files changed, 15 insertions, 4 deletions
diff --git a/av.h b/av.h
index f8c9d1bdc2..d0bbebd52b 100644
--- a/av.h
+++ b/av.h
@@ -29,7 +29,7 @@ struct xpvav {
* real if the array needs to be modified in some way. Functions that
* modify fake AVs check both flags to call av_reify() as appropriate.
*
- * Note that the Perl stack and @DB::args have neither flag set. (Thus,
+ * Note that the Perl stack has neither flag set. (Thus,
* items that go on the stack are never refcounted.)
*
* These internal details are subject to change any time. AV
diff --git a/perl.c b/perl.c
index 05cea40862..3902f22b1f 100644
--- a/perl.c
+++ b/perl.c
@@ -3779,7 +3779,7 @@ Perl_init_dbargs(pTHX)
"leak" until global destruction. */
av_clear(args);
}
- AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
+ AvREIFY_only(PL_dbargs);
}
void
diff --git a/pp_ctl.c b/pp_ctl.c
index dbec3a961c..b9bcc280a1 100644
--- a/pp_ctl.c
+++ b/pp_ctl.c
@@ -1799,7 +1799,7 @@ PP(pp_caller)
AV * const ary = cx->blk_sub.argarray;
const int off = AvARRAY(ary) - AvALLOC(ary);
- if (!PL_dbargs)
+ if (!PL_dbargs || AvREAL(PL_dbargs))
Perl_init_dbargs(aTHX);
if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
diff --git a/t/op/caller.t b/t/op/caller.t
index b857322a48..196e70e953 100644
--- a/t/op/caller.t
+++ b/t/op/caller.t
@@ -5,7 +5,7 @@ BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
- plan( tests => 80 );
+ plan( tests => 81 );
}
my @c;
@@ -205,6 +205,17 @@ EOP
}
}
+# This also used to leak [perl #97010]:
+{
+ my $gone;
+ sub fwib::DESTROY { ++$gone }
+ package DB;
+ sub { () = caller(0) }->(); # initialise PL_dbargs
+ @args = bless[],'fwib';
+ sub { () = caller(0) }->(); # clobber @args without initialisation
+ ::is $gone, 1, 'caller does not leak @DB::args elems when AvREAL';
+}
+
$::testing_caller = 1;
do './op/caller.pl' or die $@;