Skip to content

Commit fc3df28

Browse files
committed
Zend: Fix memory leak in ++/-- when overloading fetch access
Closes GH-11859
1 parent 3586264 commit fc3df28

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Overloaded array access with pre increment/decrement
3+
--FILE--
4+
<?php
5+
set_error_handler(function($severity, $m) {
6+
if (str_starts_with($m, 'Indirect modification of overloaded element')) { return; }
7+
throw new Exception($m, $severity);
8+
});
9+
class Foo implements ArrayAccess {
10+
function offsetGet($index): mixed {
11+
return range(1, 5);
12+
}
13+
function offsetSet($index, $newval): void {
14+
}
15+
function offsetExists($index): bool {
16+
return true;
17+
}
18+
function offsetUnset($index): void {
19+
}
20+
}
21+
$foo = new Foo;
22+
try {
23+
$foo[0]++;
24+
} catch (Throwable $ex) {
25+
echo $ex->getMessage() . "\n";
26+
}
27+
$foo = new Foo;
28+
try {
29+
$foo[0]--;
30+
} catch (Throwable $ex) {
31+
echo $ex->getMessage() . "\n";
32+
}
33+
?>
34+
--EXPECT--
35+
Cannot increment array
36+
Cannot decrement array

Zend/zend_vm_def.h

-14
Original file line numberDiff line numberDiff line change
@@ -1500,13 +1500,6 @@ ZEND_VM_HELPER(zend_pre_inc_helper, VAR|CV, ANY)
15001500
}
15011501
}
15021502
increment_function(var_ptr);
1503-
if (UNEXPECTED(EG(exception))) {
1504-
/* Smart branch expects result to be set with exceptions */
1505-
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1506-
ZVAL_NULL(EX_VAR(opline->result.var));
1507-
}
1508-
HANDLE_EXCEPTION();
1509-
}
15101503
} while (0);
15111504

15121505
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
@@ -1559,13 +1552,6 @@ ZEND_VM_HELPER(zend_pre_dec_helper, VAR|CV, ANY)
15591552
}
15601553
}
15611554
decrement_function(var_ptr);
1562-
if (UNEXPECTED(EG(exception))) {
1563-
/* Smart branch expects result to be set with exceptions */
1564-
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1565-
ZVAL_NULL(EX_VAR(opline->result.var));
1566-
}
1567-
HANDLE_EXCEPTION();
1568-
}
15691555
} while (0);
15701556

15711557
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {

Zend/zend_vm_execute.h

-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)