Permalink
Browse files

Fixed bug #62991 (Segfault with generator and closure)

  • Loading branch information...
1 parent 2a57a25 commit 72473962a9722ee0a8107909f32c145d3b44f906 @dstogov dstogov committed Sep 5, 2012
Showing with 64 additions and 0 deletions.
  1. +50 −0 Zend/tests/bug62991.phpt
  2. +14 −0 Zend/zend_generators.c
@@ -0,0 +1,50 @@
+--TEST--
+Bug #62991 (Segfault with generator and closure)
+--FILE--
+<?php
+
+function test( array $array )
+{
+ $closure = function() use ( $array ) {
+ print_r( $array );
+ yield "hi";
+ };
+ return $closure();
+}
+
+function test2( array $array )
+{
+ $closure = function() use ( $array ) {
+ print_r( $array );
+ yield "hi";
+ };
+ return $closure; // if you return the $closure and call it outside this function it works.
+}
+
+$generator = test(array( 1, 2, 3 ) );
+foreach($generator as $something) {
+}
+
+$generator = test2(array( 1, 2, 3 ) );
+foreach($generator() as $something) {
+}
+
+
+$generator = test2(array( 1, 2, 3 ) );
+
+echo "okey\n";
+?>
+--EXPECT--
+Array
+(
+ [0] => 1
+ [1] => 2
+ [2] => 3
+)
+Array
+(
+ [0] => 1
+ [1] => 2
+ [2] => 3
+)
+okey
@@ -154,6 +154,12 @@ void zend_generator_close(zend_generator *generator, zend_bool finished_executio
efree(prev_execute_data);
}
+ /* Free a clone of closure */
+ if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
+ destroy_op_array(op_array TSRMLS_CC);
+ efree(op_array);
+ }
+
efree(execute_data);
generator->execute_data = NULL;
}
@@ -358,6 +364,14 @@ zval *zend_generator_create_zval(zend_op_array *op_array TSRMLS_DC) /* {{{ */
zval *return_value;
zend_generator *generator;
+ /* Create a clone of closure, because it may be destroyed */
+ if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
+ zend_op_array *op_array_copy = (zend_op_array*)emalloc(sizeof(zend_op_array));
+ *op_array_copy = *op_array;
+ function_add_ref(op_array_copy);
+ op_array = op_array_copy;
+ }
+
/* Create new execution context. We have to back up and restore
* EG(current_execute_data) and EG(opline_ptr) here because the function
* modifies it. */

0 comments on commit 7247396

Please sign in to comment.