Skip to content

Commit aa394e7

Browse files
committed
Fix bug #67111
Loop variables need to be freed for both "break" and "continue". I'm adding the test to Zend/ because it's good to have a test for this even without opcache.
1 parent 5fc2fed commit aa394e7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ PHP NEWS
3131
- Mcrypt:
3232
. Fixed possible read after end of buffer and use after free. (Dmitry)
3333

34+
- Opcache:
35+
. Fixed bug #67111 (Memory leak when using "continue 2" inside two foreach
36+
loops). (Nikita)
37+
3438
- Pcntl:
3539
. Fixed bug #60509 (pcntl_signal doesn't decrease ref-count of old handler
3640
when setting SIG_DFL). (Julien)

Zend/tests/bug67111.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #67111: Memory leak when using "continue 2" inside two foreach loops
3+
--FILE--
4+
<?php
5+
6+
$array1 = [1, 2, 3];
7+
$array2 = [1, 2, 3];
8+
9+
foreach ($array1 as $x) {
10+
foreach ($array2 as $y) {
11+
echo "$x.$y\n";
12+
continue 2;
13+
}
14+
}
15+
16+
?>
17+
--EXPECT--
18+
1.1
19+
2.1
20+
3.1

ext/opcache/Optimizer/pass2.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ if (ZEND_OPTIMIZER_PASS_2 & OPTIMIZATION_LEVEL) {
175175
jmp_to = &op_array->brk_cont_array[array_offset];
176176
array_offset = jmp_to->parent;
177177
if (--nest_levels > 0) {
178-
if (opline->opcode == ZEND_BRK &&
179-
(op_array->opcodes[jmp_to->brk].opcode == ZEND_FREE ||
180-
op_array->opcodes[jmp_to->brk].opcode == ZEND_SWITCH_FREE)) {
178+
if (op_array->opcodes[jmp_to->brk].opcode == ZEND_FREE ||
179+
op_array->opcodes[jmp_to->brk].opcode == ZEND_SWITCH_FREE
180+
) {
181181
dont_optimize = 1;
182182
break;
183183
}

0 commit comments

Comments
 (0)