Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Zend/tests/gh24989.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
GH-24989 (Use-after-free in GC when an exception is thrown)
--CREDITS--
vi3tL0u1s
--FILE--
<?php
class D { function __destruct() { gc_collect_cycles(); } }

function loop_var() {
foreach (range(1, 2) as $v) {
$x = [new D, throw new Exception('loop')];
}
}

function tmp_var() {
$x = [range(1, 2), [new D, throw new Exception('tmp')]];
}

foreach (['loop_var', 'tmp_var'] as $f) {
try {
$f();
} catch (Exception $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}
?>
--EXPECT--
Exception: loop
Exception: tmp
16 changes: 16 additions & 0 deletions Zend/tests/gh24989_uncaught.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
GH-24989 (Use-after-free in GC when an exception is thrown, uncaught)
--CREDITS--
vi3tL0u1s
--FILE--
<?php
class D { function __destruct() { gc_collect_cycles(); } }
foreach (range(1, 2) as $v) {
$x = [new D, throw new Exception];
}
?>
--EXPECTF--
Fatal error: Uncaught Exception in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
10 changes: 8 additions & 2 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -4713,7 +4713,10 @@ static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num,
}

if (kind == ZEND_LIVE_TMPVAR) {
zval_ptr_dtor_nogc(var);
zval tmp;
ZVAL_COPY_VALUE(&tmp, var);
ZVAL_UNDEF(var);
zval_ptr_dtor_nogc(&tmp);
} else if (kind == ZEND_LIVE_NEW) {
zend_object *obj;
ZEND_ASSERT(Z_TYPE_P(var) == IS_OBJECT);
Expand All @@ -4724,7 +4727,10 @@ static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num,
if (Z_TYPE_P(var) != IS_ARRAY && Z_FE_ITER_P(var) != (uint32_t)-1) {
zend_hash_iterator_del(Z_FE_ITER_P(var));
}
zval_ptr_dtor_nogc(var);
zval tmp;
ZVAL_COPY_VALUE(&tmp, var);
ZVAL_UNDEF(var);
zval_ptr_dtor_nogc(&tmp);
} else if (kind == ZEND_LIVE_ROPE) {
zend_string **rope = (zend_string **)var;
zend_op *last = EX(func)->op_array.opcodes + op_num;
Expand Down
Loading