Change 31447 by [EMAIL PROTECTED] on 2007/06/22 23:46:36
safely handle cloning a stale lexical var
This code:
my $x if ...; sub { $x}
could attempt to clone $x even if $x is stale.
Affected files ...
... //depot/perl/pad.c#115 edit
... //depot/perl/t/op/closure.t#30 edit
Differences ...
==== //depot/perl/pad.c#115 (text) ====
Index: perl/pad.c
--- perl/pad.c#114~31203~ 2007-05-12 15:17:40.000000000 -0700
+++ perl/pad.c 2007-06-22 16:46:36.000000000 -0700
@@ -1501,8 +1501,8 @@
"Variable \"%s\" is not available",
SvPVX_const(namesv));
sv = NULL;
}
- else {
- assert(!SvPADSTALE(sv));
+ /* 'my $x if $y' can leave $x stale even in an active sub */
+ else if (!SvPADSTALE(sv)) {
SvREFCNT_inc_simple_void_NN(sv);
}
}
==== //depot/perl/t/op/closure.t#30 (xtext) ====
Index: perl/t/op/closure.t
--- perl/t/op/closure.t#29~26428~ 2005-12-20 12:50:26.000000000 -0800
+++ perl/t/op/closure.t 2007-06-22 16:46:36.000000000 -0700
@@ -14,7 +14,7 @@
use Config;
require './test.pl'; # for runperl()
-print "1..187\n";
+print "1..188\n";
my $test = 1;
sub test (&) {
@@ -688,6 +688,24 @@
test { $flag == 1 };
}
+# although the 'my $x if ...' form is deprecated, it must still work.
+# Ensure that cloning a stale var gives a new undef value rather than
+# sharing the old value
+
+{
+ sub f {
+ my $a = 1 if $_[0];
+ return sub { \$a };
+ }
+ my $c1 = f(1);
+ my $c2 = f(0);
+ my $r1 = $c1->();
+ my $r2 = $c2->();
+ warn "r1=$r1 r2=$r2\n";
+ test { !defined $$r2 };
+ test { $r1 ne $r2 };
+}
+
End of Patch.