This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #77930] cx_stack reallocation during sort
authorFather Chrysostomos <sprout@cpan.org>
Mon, 20 Sep 2010 08:23:35 +0000 (10:23 +0200)
committerRafael Garcia-Suarez <rgs@consttype.org>
Mon, 20 Sep 2010 08:23:35 +0000 (10:23 +0200)
Reset cx in pp_sort before POPSUB, as the pointer may no
longer be valid.

pp_sort.c
t/op/sort.t

index f1ec82a..f96d568 100644 (file)
--- a/pp_sort.c
+++ b/pp_sort.c
@@ -1679,6 +1679,9 @@ PP(pp_sort)
 
            if (!(flags & OPf_SPECIAL)) {
                SV *sv;
+               /* Reset cx, in case the context stack has been
+                  reallocated. */
+               cx = &cxstack[cxstack_ix];
                POPSUB(cx, sv);
                LEAVESUB(sv);
            }
index f92cc95..59b8321 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
     require 'test.pl';
 }
 use warnings;
-plan( tests => 159 );
+plan( tests => 160 );
 
 # these shouldn't hang
 {
@@ -890,3 +890,21 @@ fresh_perl_is('sub w ($$) {my ($l, my $r) = @_; my $v = \@_; undef @_; @_ = 0..2
 
     is($count, 0, 'all gone');
 }
+
+# [perl #77930] The context stack may be reallocated during a sort, as a
+#               result of deeply-nested (or not-so-deeply-nested) calls
+#               from a custom sort subroutine.
+fresh_perl_is
+ '
+   $sub = sub {
+    local $count = $count+1;
+    ()->$sub if $count < 1000;
+    $a cmp $b
+   };
+   () = sort $sub qw<a b c d e f g>;
+   print "ok"
+ ',
+ 'ok',
+  {},
+ '[perl #_____] cx_stack reallocation during sort'
+;