Skip to content

Commit a0ab4ad

Browse files
committed
Fix Closure::bindTo()
1 parent cbf9712 commit a0ab4ad

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Closure::bindTo() should preserve used variables
3+
--FILE--
4+
<?php
5+
6+
$var = 0;
7+
$fn = function() use($var) {
8+
var_dump($var);
9+
};
10+
$fn();
11+
$fn = $fn->bindTo(null, null);
12+
$fn();
13+
14+
?>
15+
--EXPECT--
16+
int(0)
17+
int(0)

Zend/zend_closures.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,6 @@ static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */
517517

518518
zend_create_closure(&result, &closure->func,
519519
closure->func.common.scope, closure->called_scope, &closure->this_ptr);
520-
if (closure->static_variables) {
521-
zend_closure *new_closure = (zend_closure *) Z_OBJ(result);
522-
zend_array_destroy(new_closure->static_variables);
523-
new_closure->static_variables = zend_array_dup(closure->static_variables);
524-
}
525520
return Z_OBJ(result);
526521
}
527522
/* }}} */
@@ -683,9 +678,12 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
683678
closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
684679

685680
if (closure->func.op_array.static_variables) {
681+
HashTable *static_variables =
682+
ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
686683
ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr,
687684
&closure->static_variables);
688-
closure->static_variables = zend_array_dup(closure->func.op_array.static_variables);
685+
closure->static_variables = zend_array_dup(
686+
static_variables ? static_variables : closure->func.op_array.static_variables);
689687
}
690688

691689
/* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */

0 commit comments

Comments
 (0)