Skip to content

Commit f3c1726

Browse files
committed
Merge branch 'PHP-7.1' into PHP-7.2
2 parents be0620a + 2023346 commit f3c1726

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

Zend/tests/bug75079.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #75079: self keyword leads to incorrectly generated TypeError when in closure in trait
3+
--FILE--
4+
<?php
5+
6+
trait Foo
7+
{
8+
public function selfDo(self ...$Selfs)
9+
{
10+
array_map(
11+
function (self $Self) : self
12+
{
13+
return $Self;
14+
},
15+
$Selfs
16+
);
17+
}
18+
}
19+
20+
class Bar
21+
{
22+
use Foo;
23+
}
24+
25+
class Baz
26+
{
27+
use Foo;
28+
}
29+
30+
$Bar = new Bar;
31+
$Baz = new Baz;
32+
33+
$Bar->selfDo($Bar, $Bar);
34+
$Baz->selfDo($Baz, $Baz);
35+
36+
?>
37+
===DONE===
38+
--EXPECT--
39+
===DONE===

Zend/tests/bug75079_2.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Bug #75079 variation without traits
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
private static $bar = 123;
9+
10+
static function test(){
11+
return function(){
12+
return function(){
13+
return Foo::$bar;
14+
};
15+
};
16+
}
17+
}
18+
19+
20+
$f = Foo::test();
21+
22+
var_dump($f()());
23+
24+
class A{}
25+
$a = new A;
26+
var_dump($f->bindTo($a, A::CLASS)()());
27+
28+
?>
29+
--EXPECTF--
30+
int(123)
31+
32+
Fatal error: Uncaught Error: Cannot access private property Foo::$bar in %s:%d
33+
Stack trace:
34+
#0 %s(%d): A->{closure}()
35+
#1 {main}
36+
thrown in %s on line %d

Zend/zend_closures.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ ZEND_METHOD(Closure, call)
183183
ZEND_METHOD(Closure, bind)
184184
{
185185
zval *newthis, *zclosure, *scope_arg = NULL;
186-
zend_closure *closure, *new_closure;
186+
zend_closure *closure;
187187
zend_class_entry *ce, *called_scope;
188188

189189
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
@@ -223,15 +223,6 @@ ZEND_METHOD(Closure, bind)
223223
}
224224

225225
zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
226-
new_closure = (zend_closure *) Z_OBJ_P(return_value);
227-
228-
/* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
229-
if (ZEND_USER_CODE(closure->func.type) && (closure->func.common.scope != new_closure->func.common.scope || (closure->func.op_array.fn_flags & ZEND_ACC_NO_RT_ARENA))) {
230-
new_closure->func.op_array.run_time_cache = emalloc(new_closure->func.op_array.cache_size);
231-
memset(new_closure->func.op_array.run_time_cache, 0, new_closure->func.op_array.cache_size);
232-
233-
new_closure->func.op_array.fn_flags |= ZEND_ACC_NO_RT_ARENA;
234-
}
235226
}
236227
/* }}} */
237228

@@ -666,9 +657,24 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
666657
closure->func.op_array.static_variables =
667658
zend_array_dup(closure->func.op_array.static_variables);
668659
}
669-
if (UNEXPECTED(!closure->func.op_array.run_time_cache)) {
670-
closure->func.op_array.run_time_cache = func->op_array.run_time_cache = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
671-
memset(func->op_array.run_time_cache, 0, func->op_array.cache_size);
660+
661+
/* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
662+
if (!closure->func.op_array.run_time_cache
663+
|| func->common.scope != scope
664+
|| (func->common.fn_flags & ZEND_ACC_NO_RT_ARENA)
665+
) {
666+
if (!func->op_array.run_time_cache && (func->common.fn_flags & ZEND_ACC_CLOSURE)) {
667+
/* If a real closure is used for the first time, we create a shared runtime cache
668+
* and remember which scope it is for. */
669+
func->common.scope = scope;
670+
func->op_array.run_time_cache = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
671+
closure->func.op_array.run_time_cache = func->op_array.run_time_cache;
672+
} else {
673+
/* Otherwise, we use a non-shared runtime cache */
674+
closure->func.op_array.run_time_cache = emalloc(func->op_array.cache_size);
675+
closure->func.op_array.fn_flags |= ZEND_ACC_NO_RT_ARENA;
676+
}
677+
memset(closure->func.op_array.run_time_cache, 0, func->op_array.cache_size);
672678
}
673679
if (closure->func.op_array.refcount) {
674680
(*closure->func.op_array.refcount)++;

0 commit comments

Comments
 (0)