Skip to content

Commit 1979c06

Browse files
committed
static var overriding closure binding should error
1 parent ee32155 commit 1979c06

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Static variable can't override bound closure variables
3+
--FILE--
4+
<?php
5+
6+
$a = null;
7+
8+
function () use (&$a) {
9+
static $a;
10+
};
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Duplicate declaration of static variable $a in %s on line %d

Zend/zend_compile.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,25 +4876,22 @@ static void zend_compile_static_var(zend_ast *ast) /* {{{ */
48764876
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
48774877
}
48784878

4879+
if (!CG(active_op_array)->static_variables) {
4880+
if (CG(active_op_array)->scope) {
4881+
CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
4882+
}
4883+
CG(active_op_array)->static_variables = zend_new_array(8);
4884+
}
4885+
4886+
if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
4887+
zend_error_noreturn(E_COMPILE_ERROR, "Duplicate declaration of static variable $%s", ZSTR_VAL(var_name));
4888+
}
4889+
48794890
if (!value_ast) {
48804891
zend_compile_static_var_common(var_name, &EG(uninitialized_zval), ZEND_BIND_REF);
48814892
} else {
48824893
zend_op *opline;
48834894

4884-
if (!CG(active_op_array)->static_variables) {
4885-
if (CG(active_op_array)->scope) {
4886-
CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
4887-
}
4888-
CG(active_op_array)->static_variables = zend_new_array(8);
4889-
}
4890-
4891-
if (zend_string_equals_literal(var_name, "this")) {
4892-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
4893-
}
4894-
if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
4895-
zend_error_noreturn(E_COMPILE_ERROR, "Duplicate declaration of static variable $%s", ZSTR_VAL(var_name));
4896-
}
4897-
48984895
zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
48994896
Z_TYPE_EXTRA_P(placeholder_ptr) |= IS_TYPE_UNINITIALIZED;
49004897
uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);

0 commit comments

Comments
 (0)