com php-src: Fix generator being a cycle to itself: Zend/tests/generators/gc_with_yield_from.phpt Zend/ze nd_generators.c
| From: | Bob Weinand | Date: | Mon, 18 Apr 2016 21:55:16 +0000 |
| Subject: | com php-src: Fix generator being a cycle to itself: Zend/tests/generators/gc_with_yield_from.phpt Zend/ze nd_generators.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-91782@lists.php.net to get a copy of this message | ||
Commit: 5085352464e55e92cd5bb7beeb2284b2e1b78061
Author: Bob Weinand <bobwei9@hotmail.com> Mon, 18 Apr 2016 23:54:41 +0200
Parents: ea1f30057b50b24c83750b8d5410108b7ba65180
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=5085352464e55e92cd5bb7beeb2284b2e1b78061
Log:
Fix generator being a cycle to itself
Changed paths:
A Zend/tests/generators/gc_with_yield_from.phpt
M Zend/zend_generators.c
Diff:
diff --git a/Zend/tests/generators/gc_with_yield_from.phpt
b/Zend/tests/generators/gc_with_yield_from.phpt
new file mode 100644
index 0000000..8c05561
--- /dev/null
+++ b/Zend/tests/generators/gc_with_yield_from.phpt
@@ -0,0 +1,45 @@
+--TEST--
+Verify yield from on generators being properly cycle collected
+--FILE--
+<?php
+
+function root() {
+ global $gens; // create cyclic reference to root
+ try {
+ yield 1;
+ } finally {
+ var_dump($gens);
+ }
+}
+
+function gen($x) {
+ global $gens;
+ yield from $gens[] = $x ? gen(--$x) : root();
+}
+
+$gen = $gens[] = gen(2);
+var_dump($gen->current());
+unset($gen, $gens);
+print "collect\n";
+gc_collect_cycles();
+print "end\n";
+
+?>
+--EXPECT--
+int(1)
+collect
+array(4) {
+ [0]=>
+ object(Generator)#1 (0) {
+ }
+ [1]=>
+ object(Generator)#2 (0) {
+ }
+ [2]=>
+ object(Generator)#3 (0) {
+ }
+ [3]=>
+ object(Generator)#4 (0) {
+ }
+}
+end
diff --git a/Zend/zend_generators.c b/Zend/zend_generators.c
index e2cc238..33f187c 100644
--- a/Zend/zend_generators.c
+++ b/Zend/zend_generators.c
@@ -220,10 +220,10 @@ static uint32_t calc_gc_buffer_size(zend_generator *generator) /* {{{ */
/* Yield from root references */
if (generator->node.children == 0) {
- zend_generator *root = generator->node.ptr.root;
- while (root != generator) {
+ zend_generator *child = generator, *root = generator->node.ptr.root;
+ while (root != child) {
+ child = child->node.parent;
size++;
- root = zend_generator_get_child(&root->node, generator);
}
}
}
@@ -287,8 +287,8 @@ static HashTable *zend_generator_get_gc(zval *object, zval **table, int *n) /* {
if (generator->node.children == 0) {
zend_generator *child = generator, *root = generator->node.ptr.root;
while (root != child) {
- ZVAL_OBJ(gc_buffer++, &child->std);
child = child->node.parent;
+ ZVAL_OBJ(gc_buffer++, &child->std);
}
}