Skip to content

Commit 71a29c3

Browse files
author
Gurusamy Sarathy
committed
fix sort optimizer to not hang inside loops
p4raw-id: //depot/perl@5815
1 parent 3dcd9d3 commit 71a29c3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

op.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5999,6 +5999,12 @@ Perl_ck_sort(pTHX_ OP *o)
59995999
for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
60006000
if (k->op_next == kid)
60016001
k->op_next = 0;
6002+
/* don't descend into loops */
6003+
else if (k->op_type == OP_ENTERLOOP
6004+
|| k->op_type == OP_ENTERITER)
6005+
{
6006+
k = cLOOPx(k)->op_lastop;
6007+
}
60026008
}
60036009
}
60046010
else

t/op/sort.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ print "1..49\n";
1313
$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
1414
}
1515

16+
# these shouldn't hang
17+
{
18+
no warnings;
19+
sort { for ($_ = 0;; $_++) {} } @a;
20+
sort { while(1) {} } @a;
21+
sort { while(1) { last; } } @a;
22+
sort { while(0) { last; } } @a;
23+
}
24+
1625
sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
1726
sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
1827

0 commit comments

Comments
 (0)