Skip to content

Commit 6cc5398

Browse files
committed
Addref static vars when not copying private method
While we don't need to give this method separate static vars, we do still need to perform an addref, as there will be a corresponding delref in the dtor.
1 parent a10093e commit 6cc5398

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Behavior of static variable in private trait method
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
private static function method() {
8+
static $x;
9+
if ($x === null) $x = new stdClass;
10+
return $x;
11+
}
12+
13+
public static function method2() {
14+
return self::method();
15+
}
16+
}
17+
18+
class C {
19+
use T;
20+
}
21+
22+
var_dump(C::method2());
23+
24+
class D extends C {
25+
use T;
26+
}
27+
28+
var_dump(D::method2());
29+
30+
?>
31+
--EXPECT--
32+
object(stdClass)#1 (0) {
33+
}
34+
object(stdClass)#2 (0) {
35+
}

Zend/zend_inheritance.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,19 @@ static zend_always_inline zend_function *zend_duplicate_function(zend_function *
9696
(*func->op_array.refcount)++;
9797
}
9898
if (is_interface
99-
|| EXPECTED(!func->op_array.static_variables)
100-
|| (func->op_array.fn_flags & ZEND_ACC_PRIVATE)) {
99+
|| EXPECTED(!func->op_array.static_variables)) {
101100
/* reuse the same op_array structure */
102101
return func;
103102
}
103+
if (func->op_array.fn_flags & ZEND_ACC_PRIVATE) {
104+
/* For private methods we reuse the same op_array structure even if
105+
* static variables are used, because it will not end up being used
106+
* anyway. However we still need to addref as the dtor will delref. */
107+
if (!(GC_FLAGS(func->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
108+
GC_ADDREF(func->op_array.static_variables);
109+
}
110+
return func;
111+
}
104112
return zend_duplicate_user_function(func);
105113
}
106114
}

0 commit comments

Comments
 (0)