fix sort optimizer to not hang inside loops
Gurusamy Sarathy [Sun, 19 Mar 2000 05:27:31 +0000 (05:27 +0000)]
p4raw-id: //depot/perl@5815

op.c
t/op/sort.t

diff --git a/op.c b/op.c
index d162595..97d2e4b 100644 (file)
--- a/op.c
+++ b/op.c
@@ -5999,6 +5999,12 @@ Perl_ck_sort(pTHX_ OP *o)
                    for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
                        if (k->op_next == kid)
                            k->op_next = 0;
+                       /* don't descend into loops */
+                       else if (k->op_type == OP_ENTERLOOP
+                                || k->op_type == OP_ENTERITER)
+                       {
+                           k = cLOOPx(k)->op_lastop;
+                       }
                    }
                }
                else
index 794b1f2..ba0a4c2 100755 (executable)
@@ -13,6 +13,15 @@ print "1..49\n";
   $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
 }
 
+# these shouldn't hang
+{
+    no warnings;
+    sort { for ($_ = 0;; $_++) {} } @a;
+    sort { while(1) {}            } @a;
+    sort { while(1) { last; }     } @a;
+    sort { while(0) { last; }     } @a;
+}
+
 sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
 sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }