Skip to content

Commit fc93206

Browse files
committed
Fix static expressions to not allow constants
1 parent a42c73f commit fc93206

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Class Property Expressions Failure
3+
--FILE--
4+
<?php
5+
class Foo {
6+
const BAR = 1 << 0;
7+
const BAZ = 1 << 1;
8+
public $bar = self::BAR | self::BAZ;
9+
}
10+
?>
11+
--EXPECTF--
12+
Parse error: syntax error, unexpected '|', expecting ',' or ';' in %s/class_properties_dynamic-failure.php on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Constant Expressions Failure
3+
--FILE--
4+
<?php
5+
const FOO = 1;
6+
const BAR = FOO | 1;
7+
?>
8+
--EXPECTF--
9+
Parse error: syntax error, unexpected '|', expecting ',' or ';' in %s/constant-expressions-failure.php on line %d

Zend/zend_language_parser.y

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -943,35 +943,39 @@ common_scalar:
943943

944944

945945
static_scalar: /* compile-time evaluated scalars */
946-
common_scalar { $$ = $1; }
946+
static_scalar_value { $$ = $1; }
947947
| static_class_name_scalar { $$ = $1; }
948948
| namespace_name { zend_do_fetch_constant(&$$, NULL, &$1, ZEND_CT, 1 TSRMLS_CC); }
949949
| 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_CT, 0 TSRMLS_CC); }
950950
| 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_CT, 0 TSRMLS_CC); }
951-
| '+' static_scalar { ZVAL_LONG(&$1.u.constant, 0); add_function(&$2.u.constant, &$1.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
952-
| '-' static_scalar { ZVAL_LONG(&$1.u.constant, 0); sub_function(&$2.u.constant, &$1.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
953951
| T_ARRAY '(' static_array_pair_list ')' { $$ = $3; Z_TYPE($$.u.constant) = IS_CONSTANT_ARRAY; }
954952
| '[' static_array_pair_list ']' { $$ = $2; Z_TYPE($$.u.constant) = IS_CONSTANT_ARRAY; }
955953
| static_class_constant { $$ = $1; }
956954
| T_CLASS_C { $$ = $1; }
957-
| '(' static_scalar ')' { $$ = $2; }
955+
;
956+
957+
static_scalar_value:
958+
common_scalar { $$ = $1; }
959+
| '+' static_scalar_value { ZVAL_LONG(&$1.u.constant, 0); add_function(&$2.u.constant, &$1.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
960+
| '-' static_scalar_value { ZVAL_LONG(&$1.u.constant, 0); sub_function(&$2.u.constant, &$1.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
961+
| '(' static_scalar_value ')' { $$ = $2; }
958962
| static_operation { $$ = $1; }
959963
;
960964

961965
static_operation:
962-
static_scalar '+' static_scalar { add_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
963-
| static_scalar '-' static_scalar { sub_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
964-
| static_scalar '*' static_scalar { mul_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
965-
| static_scalar '/' static_scalar { div_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
966-
| static_scalar '%' static_scalar { mod_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
967-
| '!' static_scalar { boolean_not_function(&$2.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
968-
| '~' static_scalar { bitwise_not_function(&$2.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
969-
| static_scalar '|' static_scalar { bitwise_or_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
970-
| static_scalar '&' static_scalar { bitwise_and_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
971-
| static_scalar '^' static_scalar { bitwise_xor_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
972-
| static_scalar T_SL static_scalar { shift_left_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
973-
| static_scalar T_SR static_scalar { shift_right_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
974-
| static_scalar '.' static_scalar { concat_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
966+
static_scalar_value '+' static_scalar_value { add_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
967+
| static_scalar_value '-' static_scalar_value { sub_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
968+
| static_scalar_value '*' static_scalar_value { mul_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
969+
| static_scalar_value '/' static_scalar_value { div_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
970+
| static_scalar_value '%' static_scalar_value { mod_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
971+
| '!' static_scalar_value { boolean_not_function(&$2.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
972+
| '~' static_scalar_value { bitwise_not_function(&$2.u.constant, &$2.u.constant TSRMLS_CC); $$ = $2; }
973+
| static_scalar_value '|' static_scalar_value { bitwise_or_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
974+
| static_scalar_value '&' static_scalar_value { bitwise_and_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
975+
| static_scalar_value '^' static_scalar_value { bitwise_xor_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
976+
| static_scalar_value T_SL static_scalar_value { shift_left_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
977+
| static_scalar_value T_SR static_scalar_value { shift_right_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
978+
| static_scalar_value '.' static_scalar_value { concat_function(&$1.u.constant, &$1.u.constant, &$3.u.constant TSRMLS_CC); zval_dtor(&$3.u.constant); $$ = $1; }
975979
;
976980

977981
static_class_constant:

0 commit comments

Comments
 (0)