Skip to content
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

Fix GH-7958: Nested CallbackFilterIterator is leaking memory #8107

Closed
wants to merge 2 commits 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
43 changes: 43 additions & 0 deletions Zend/tests/gh7958.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
GH-7958 (Nested CallbackFilterIterator is leaking memory)
--FILE--
<?php
class Action
{
public \Iterator $iterator;

public function __construct(array $data)
{
$this->iterator = new ArrayIterator($data);
echo '-- c ' . spl_object_id($this) . "\n";
}

public function __destruct()
{
echo '-- d ' . spl_object_id($this) . "\n";
}

public function filter()
{
$this->iterator = new \CallbackFilterIterator($this->iterator, fn() => true);
$this->iterator->rewind();
}
}

$action = new Action(['a', 'b']);
$action->filter();
$action->filter();
print_r(iterator_to_array($action->iterator));
$action = null;
gc_collect_cycles();
echo "==DONE==\n";
?>
--EXPECT--
-- c 1
Array
(
[0] => a
[1] => b
)
-- d 1
==DONE==
10 changes: 9 additions & 1 deletion Zend/zend_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ ZEND_API void zend_user_it_rewind(zend_object_iterator *_iter)
}
/* }}} */

ZEND_API HashTable *zend_user_it_get_gc(zend_object_iterator *_iter, zval **table, int *n)
{
zend_user_iterator *iter = (zend_user_iterator*)_iter;
*table = &iter->it.data;
*n = 1;
return NULL;
}

static const zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
zend_user_it_dtor,
zend_user_it_valid,
Expand All @@ -190,7 +198,7 @@ static const zend_object_iterator_funcs zend_interface_iterator_funcs_iterator =
zend_user_it_move_forward,
zend_user_it_rewind,
zend_user_it_invalidate_current,
NULL, /* get_gc */
zend_user_it_get_gc,
};

/* {{{ zend_user_it_get_iterator */
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ZEND_API void zend_user_it_get_current_key(zend_object_iterator *_iter, zval *ke
ZEND_API zval *zend_user_it_get_current_data(zend_object_iterator *_iter);
ZEND_API void zend_user_it_move_forward(zend_object_iterator *_iter);
ZEND_API void zend_user_it_invalidate_current(zend_object_iterator *_iter);
ZEND_API HashTable *zend_user_it_get_gc(zend_object_iterator *_iter, zval **table, int *n);

ZEND_API void zend_user_it_new_iterator(zend_class_entry *ce, zval *object, zval *iterator);
ZEND_API zend_object_iterator *zend_user_it_get_new_iterator(zend_class_entry *ce, zval *object, int by_ref);
Expand Down