Skip to content

Fix memory leak in by-ref foreach. #12538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ PHP NEWS
. Fixed bug GH-12468 (Double-free of doc_comment when overriding static
property via trait). (ilutov)
. Fixed segfault caused by weak references to FFI objects. (sj-i)
. Fix memory leak if a circular reference is created inside a by-ref foreach
on the array variable. (danog)

- DOM:
. Fix registerNodeClass with abstract class crashing. (nielsdos)
Expand Down
17 changes: 17 additions & 0 deletions Zend/tests/leak_foreach_on_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Fix memory leak if a circular reference is created inside a by-ref foreach on the array variable.
--FILE--
<?php

$a = [0];

foreach ($a as &$v) {
$a[0] = &$a;
unset($a);
gc_collect_cycles();
}

?>
==DONE==
--EXPECT--
==DONE==
2 changes: 1 addition & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3125,7 +3125,7 @@ ZEND_VM_HOT_HANDLER(127, ZEND_FE_FREE, TMPVAR, ANY)
if (Z_FE_ITER_P(var) != (uint32_t)-1) {
zend_hash_iterator_del(Z_FE_ITER_P(var));
}
zval_ptr_dtor_nogc(var);
zval_ptr_dtor(var);
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -14317,7 +14317,7 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FE_FREE_SPEC_TMPVA
if (Z_FE_ITER_P(var) != (uint32_t)-1) {
zend_hash_iterator_del(Z_FE_ITER_P(var));
}
zval_ptr_dtor_nogc(var);
zval_ptr_dtor(var);
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

Expand Down