Skip to content

Commit d5ddd2d

Browse files
committed
Disable restrictions regarding arrays in constants at run-time.
For the discussion around it, see the thread on the mailing list: http://www.mail-archive.com/internals@lists.php.net/msg68245.html
1 parent 354ee12 commit d5ddd2d

File tree

8 files changed

+65
-32
lines changed

8 files changed

+65
-32
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ PHP NEWS
44

55
- Core:
66
. Fixed bug #67693 (incorrect push to the empty array). (Tjerk)
7+
. Removed inconsistency regarding behaviour of array in constants at
8+
run-time. (Bob)
79

810
- SPL:
911
. Revert fix for bug #67064 (BC issues). (Bob)

Zend/tests/constant_expressions_arrays.phpt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class foo {
2222

2323
var_dump(foo::bar);
2424

25-
var_dump(a); // Eventually allow that later with array dereferencing of constants
25+
var_dump(a, a[0], a[2], a[2][1], a[3]);
2626

2727
?>
2828
--EXPECTF--
@@ -32,4 +32,35 @@ int(1)
3232
int(4)
3333
int(1)
3434

35-
Fatal error: Arrays are not allowed in constants at run-time in %s on line %d
35+
Notice: Undefined offset: 3 in %s on line %d
36+
array(3) {
37+
[0]=>
38+
int(1)
39+
[1]=>
40+
int(2)
41+
[2]=>
42+
array(2) {
43+
[0]=>
44+
int(3)
45+
[1]=>
46+
array(1) {
47+
[0]=>
48+
int(4)
49+
}
50+
}
51+
}
52+
int(1)
53+
array(2) {
54+
[0]=>
55+
int(3)
56+
[1]=>
57+
array(1) {
58+
[0]=>
59+
int(4)
60+
}
61+
}
62+
array(1) {
63+
[0]=>
64+
int(4)
65+
}
66+
NULL

Zend/tests/errmsg_040.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ var_dump(test::TEST);
1212
echo "Done\n";
1313
?>
1414
--EXPECTF--
15-
Fatal error: Arrays are not allowed in constants at run-time in %s on line %d
15+
array(3) {
16+
[0]=>
17+
int(1)
18+
[1]=>
19+
int(2)
20+
[2]=>
21+
int(3)
22+
}
23+
Done

Zend/tests/ns_059.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ const C = array();
77
var_dump(C);
88
?>
99
--EXPECTF--
10-
Fatal error: Arrays are not allowed in constants at run-time in %sns_059.php on line 4
11-
10+
array(0) {
11+
}

Zend/zend_language_parser.y

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -865,13 +865,16 @@ yield_expr:
865865
;
866866

867867
combined_scalar_offset:
868-
combined_scalar '[' dim_offset ']' { zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
869-
| combined_scalar_offset '[' dim_offset ']' { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
870-
| T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']' { $1.EA = 0; zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
868+
combined_scalar '[' dim_offset ']' { zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
869+
| combined_scalar_offset '[' dim_offset ']' { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
870+
| T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']' { $1.EA = 0; zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
871+
| general_constant '[' dim_offset ']' { zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
872+
;
871873

872874
combined_scalar:
873-
T_ARRAY '(' array_pair_list ')' { $$ = $3; }
874-
| '[' array_pair_list ']' { $$ = $2; }
875+
T_ARRAY '(' array_pair_list ')' { $$ = $3; }
876+
| '[' array_pair_list ']' { $$ = $2; }
877+
;
875878

876879
function:
877880
T_FUNCTION { $$.u.op.opline_num = CG(zend_lineno); }
@@ -1038,20 +1041,22 @@ static_operation:
10381041
| '(' static_scalar_value ')' { $$ = $2; }
10391042
;
10401043

1041-
1042-
scalar:
1043-
T_STRING_VARNAME { $$ = $1; }
1044-
| class_name_scalar { $$ = $1; }
1045-
| class_constant { $$ = $1; }
1044+
general_constant:
1045+
class_constant { $$ = $1; }
10461046
| namespace_name { zend_do_fetch_constant(&$$, NULL, &$1, ZEND_RT, 1 TSRMLS_CC); }
10471047
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $$.op_type = IS_CONST; ZVAL_EMPTY_STRING(&$$.u.constant); zend_do_build_namespace_name(&$$, &$$, &$3 TSRMLS_CC); $3 = $$; zend_do_fetch_constant(&$$, NULL, &$3, ZEND_RT, 0 TSRMLS_CC); }
10481048
| T_NS_SEPARATOR namespace_name { char *tmp = estrndup(Z_STRVAL($2.u.constant), Z_STRLEN($2.u.constant)+1); memcpy(&(tmp[1]), Z_STRVAL($2.u.constant), Z_STRLEN($2.u.constant)+1); tmp[0] = '\\'; efree(Z_STRVAL($2.u.constant)); Z_STRVAL($2.u.constant) = tmp; ++Z_STRLEN($2.u.constant); zend_do_fetch_constant(&$$, NULL, &$2, ZEND_RT, 0 TSRMLS_CC); }
1049-
| common_scalar { $$ = $1; }
1050-
| '"' encaps_list '"' { $$ = $2; }
1051-
| T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; }
1052-
| T_CLASS_C { if (Z_TYPE($1.u.constant) == IS_CONSTANT) {zend_do_fetch_constant(&$$, NULL, &$1, ZEND_RT, 1 TSRMLS_CC);} else {$$ = $1;} }
10531049
;
10541050

1051+
scalar:
1052+
T_STRING_VARNAME { $$ = $1; }
1053+
| general_constant { $$ = $1; }
1054+
| class_name_scalar { $$ = $1; }
1055+
| common_scalar { $$ = $1; }
1056+
| '"' encaps_list '"' { $$ = $2; }
1057+
| T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; }
1058+
| T_CLASS_C { if (Z_TYPE($1.u.constant) == IS_CONSTANT) {zend_do_fetch_constant(&$$, NULL, &$1, ZEND_RT, 1 TSRMLS_CC);} else {$$ = $1;} }
1059+
;
10551060

10561061
static_array_pair_list:
10571062
/* empty */ { $$.op_type = IS_CONST; INIT_PZVAL(&$$.u.constant); array_init(&$$.u.constant); $$.u.ast = zend_ast_create_constant(&$$.u.constant); }

Zend/zend_vm_def.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,9 +3772,6 @@ ZEND_VM_HANDLER(99, ZEND_FETCH_CONSTANT, VAR|CONST|UNUSED, CONST)
37723772
}
37733773
}
37743774
constant_fetch_end:
3775-
if (Z_TYPE(EX_T(opline->result.var).tmp_var) == IS_ARRAY) {
3776-
zend_error_noreturn(E_ERROR, "Arrays are not allowed in constants at run-time");
3777-
}
37783775
CHECK_EXCEPTION();
37793776
ZEND_VM_NEXT_OPCODE();
37803777
}

Zend/zend_vm_execute.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4036,9 +4036,6 @@ static int ZEND_FASTCALL ZEND_FETCH_CONSTANT_SPEC_CONST_CONST_HANDLER(ZEND_OPCO
40364036
}
40374037
}
40384038
constant_fetch_end:
4039-
if (Z_TYPE(EX_T(opline->result.var).tmp_var) == IS_ARRAY) {
4040-
zend_error_noreturn(E_ERROR, "Arrays are not allowed in constants at run-time");
4041-
}
40424039
CHECK_EXCEPTION();
40434040
ZEND_VM_NEXT_OPCODE();
40444041
}
@@ -16001,9 +15998,6 @@ static int ZEND_FASTCALL ZEND_FETCH_CONSTANT_SPEC_VAR_CONST_HANDLER(ZEND_OPCODE
1600115998
}
1600215999
}
1600316000
constant_fetch_end:
16004-
if (Z_TYPE(EX_T(opline->result.var).tmp_var) == IS_ARRAY) {
16005-
zend_error_noreturn(E_ERROR, "Arrays are not allowed in constants at run-time");
16006-
}
1600716001
CHECK_EXCEPTION();
1600816002
ZEND_VM_NEXT_OPCODE();
1600916003
}
@@ -25613,9 +25607,6 @@ static int ZEND_FASTCALL ZEND_FETCH_CONSTANT_SPEC_UNUSED_CONST_HANDLER(ZEND_OPC
2561325607
}
2561425608
}
2561525609
constant_fetch_end:
25616-
if (Z_TYPE(EX_T(opline->result.var).tmp_var) == IS_ARRAY) {
25617-
zend_error_noreturn(E_ERROR, "Arrays are not allowed in constants at run-time");
25618-
}
2561925610
CHECK_EXCEPTION();
2562025611
ZEND_VM_NEXT_OPCODE();
2562125612
}

ext/standard/array.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ PHP_FUNCTION(count)
339339
RETVAL_LONG(Z_LVAL_P(retval));
340340
zval_ptr_dtor(&retval);
341341
}
342-
zval_ptr_dtor(&mode_zv);
343342
return;
344343
}
345344
#endif

0 commit comments

Comments
 (0)