Skip to content

Commit efcb9a7

Browse files
committed
- Fixed bug #54358 (Closure, use and reference)
- Fixed bug #54039 (use() of static variables in lambda functions can break staticness)
1 parent 77ed819 commit efcb9a7

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Zend/tests/bug54039.phpt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Bug #54039 (use() of static variables in lambda functions can break staticness)
3+
--FILE--
4+
<?php
5+
function test_1() {
6+
static $v = 0;
7+
++$v;
8+
echo "Outer function increments \$v to $v\n";
9+
$f = function() use($v) {
10+
echo "Inner function reckons \$v is $v\n";
11+
};
12+
return $f;
13+
}
14+
15+
$f = test_1(); $f();
16+
$f = test_1(); $f();
17+
18+
function test_2() {
19+
static $v = 0;
20+
$f = function() use($v) {
21+
echo "Inner function reckons \$v is $v\n";
22+
};
23+
++$v;
24+
echo "Outer function increments \$v to $v\n";
25+
return $f;
26+
}
27+
28+
$f = test_2(); $f();
29+
$f = test_2(); $f();
30+
31+
function test_3() {
32+
static $v = "";
33+
$v .= 'b';
34+
echo "Outer function catenates 'b' onto \$v to give $v\n";
35+
$f = function() use($v) {
36+
echo "Inner function reckons \$v is $v\n";
37+
};
38+
$v .= 'a';
39+
echo "Outer function catenates 'a' onto \$v to give $v\n";
40+
return $f;
41+
}
42+
$f = test_3(); $f();
43+
$f = test_3(); $f();
44+
--EXPECT--
45+
Outer function increments $v to 1
46+
Inner function reckons $v is 1
47+
Outer function increments $v to 2
48+
Inner function reckons $v is 2
49+
Outer function increments $v to 1
50+
Inner function reckons $v is 0
51+
Outer function increments $v to 2
52+
Inner function reckons $v is 1
53+
Outer function catenates 'b' onto $v to give b
54+
Outer function catenates 'a' onto $v to give ba
55+
Inner function reckons $v is b
56+
Outer function catenates 'b' onto $v to give bab
57+
Outer function catenates 'a' onto $v to give baba
58+
Inner function reckons $v is bab

Zend/tests/bug54358.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #54358 (Closure, use and reference)
3+
--FILE--
4+
<?php
5+
class asserter {
6+
public function call($function) {
7+
}
8+
}
9+
10+
$asserter = new asserter();
11+
12+
$closure = function() use ($asserter, &$function) {
13+
$asserter->call($function = 'md5');
14+
};
15+
16+
$closure();
17+
18+
var_dump($function);
19+
20+
$closure = function() use ($asserter, $function) {
21+
$asserter->call($function);
22+
};
23+
24+
$closure();
25+
26+
var_dump($function);
27+
28+
$closure = function() use ($asserter, $function) {
29+
$asserter->call($function);
30+
};
31+
32+
$closure();
33+
34+
var_dump($function);
35+
?>
36+
--EXPECT--
37+
string(3) "md5"
38+
string(3) "md5"
39+
string(3) "md5"

Zend/zend_variables.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ ZEND_API int zval_copy_static_var(zval **p TSRMLS_DC, int num_args, va_list args
216216
} else if (Z_ISREF_PP(p)) {
217217
ALLOC_INIT_ZVAL(tmp);
218218
ZVAL_COPY_VALUE(tmp, *p);
219+
zval_copy_ctor(tmp);
219220
Z_SET_REFCOUNT_P(tmp, 0);
220221
Z_UNSET_ISREF_P(tmp);
221222
} else {

0 commit comments

Comments
 (0)