@@ -97,6 +97,12 @@ static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
9797}
9898/* }}} */
9999
100+ static void zend_destroy_class_constant_internal (zval * zv ) /* {{{ */
101+ {
102+ free (Z_PTR_P (zv ));
103+ }
104+ /* }}} */
105+
100106static zend_string * zend_new_interned_string_safe (zend_string * str ) /* {{{ */ {
101107 zend_string * interned_str ;
102108
@@ -1437,14 +1443,15 @@ static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_a
14371443static zend_bool zend_try_ct_eval_class_const (zval * zv , zend_string * class_name , zend_string * name ) /* {{{ */
14381444{
14391445 uint32_t fetch_type = zend_get_class_fetch_type (class_name );
1446+ zend_class_constant * cc ;
14401447 zval * c ;
14411448
14421449 if (class_name_refers_to_active_ce (class_name , fetch_type )) {
1443- c = zend_hash_find (& CG (active_class_entry )-> constants_table , name );
1450+ cc = zend_hash_find_ptr (& CG (active_class_entry )-> constants_table , name );
14441451 } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG (compiler_options ) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION )) {
14451452 zend_class_entry * ce = zend_hash_find_ptr_lc (CG (class_table ), ZSTR_VAL (class_name ), ZSTR_LEN (class_name ));
14461453 if (ce ) {
1447- c = zend_hash_find (& ce -> constants_table , name );
1454+ cc = zend_hash_find_ptr (& ce -> constants_table , name );
14481455 } else {
14491456 return 0 ;
14501457 }
@@ -1456,8 +1463,14 @@ static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name,
14561463 return 0 ;
14571464 }
14581465
1466+ if (!cc || !zend_verify_const_access (cc , CG (active_class_entry ))) {
1467+ return 0 ;
1468+ }
1469+
1470+ c = & cc -> value ;
1471+
14591472 /* Substitute case-sensitive (or lowercase) persistent class constants */
1460- if (c && Z_TYPE_P (c ) < IS_OBJECT ) {
1473+ if (Z_TYPE_P (c ) < IS_OBJECT ) {
14611474 ZVAL_DUP (zv , c );
14621475 return 1 ;
14631476 }
@@ -1638,7 +1651,6 @@ int zendlex(zend_parser_stack_elem *elem) /* {{{ */
16381651ZEND_API void zend_initialize_class_data (zend_class_entry * ce , zend_bool nullify_handlers ) /* {{{ */
16391652{
16401653 zend_bool persistent_hashes = (ce -> type == ZEND_INTERNAL_CLASS ) ? 1 : 0 ;
1641- dtor_func_t zval_ptr_dtor_func = ((persistent_hashes ) ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR );
16421654
16431655 ce -> refcount = 1 ;
16441656 ce -> ce_flags = ZEND_ACC_CONSTANTS_UPDATED ;
@@ -1650,7 +1662,7 @@ ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify
16501662 ce -> default_properties_table = NULL ;
16511663 ce -> default_static_members_table = NULL ;
16521664 zend_hash_init_ex (& ce -> properties_info , 8 , NULL , (persistent_hashes ? zend_destroy_property_info_internal : NULL ), persistent_hashes , 0 );
1653- zend_hash_init_ex (& ce -> constants_table , 8 , NULL , zval_ptr_dtor_func , persistent_hashes , 0 );
1665+ zend_hash_init_ex (& ce -> constants_table , 8 , NULL , ( persistent_hashes ? zend_destroy_class_constant_internal : NULL ) , persistent_hashes , 0 );
16541666 zend_hash_init_ex (& ce -> function_table , 8 , NULL , ZEND_FUNCTION_DTOR , persistent_hashes , 0 );
16551667
16561668 if (ce -> type == ZEND_INTERNAL_CLASS ) {
@@ -5183,25 +5195,28 @@ void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
51835195 zend_ast * const_ast = list -> child [i ];
51845196 zend_ast * name_ast = const_ast -> child [0 ];
51855197 zend_ast * value_ast = const_ast -> child [1 ];
5198+ zend_ast * doc_comment_ast = const_ast -> child [2 ];
51865199 zend_string * name = zend_ast_get_str (name_ast );
5200+ zend_string * doc_comment = doc_comment_ast ? zend_string_copy (zend_ast_get_str (doc_comment_ast )) : NULL ;
51875201 zval value_zv ;
51885202
5189- if (zend_string_equals_literal_ci (name , "class" )) {
5190- zend_error (E_COMPILE_ERROR ,
5191- "A class constant must not be called 'class'; it is reserved for class name fetching" );
5203+ if (UNEXPECTED (ast -> attr & (ZEND_ACC_STATIC |ZEND_ACC_ABSTRACT |ZEND_ACC_FINAL ))) {
5204+ if (ast -> attr & ZEND_ACC_STATIC ) {
5205+ zend_error_noreturn (E_COMPILE_ERROR , "Cannot use 'static' as constant modifier" );
5206+ } else if (ast -> attr & ZEND_ACC_ABSTRACT ) {
5207+ zend_error_noreturn (E_COMPILE_ERROR , "Cannot use 'abstract' as constant modifier" );
5208+ } else if (ast -> attr & ZEND_ACC_FINAL ) {
5209+ zend_error_noreturn (E_COMPILE_ERROR , "Cannot use 'final' as constant modifier" );
5210+ }
51925211 }
51935212
51945213 zend_const_expr_to_zval (& value_zv , value_ast );
51955214
51965215 name = zend_new_interned_string_safe (name );
5197- if (zend_hash_add ( & ce -> constants_table , name , & value_zv ) == NULL ) {
5216+ if (zend_declare_class_constant_ex ( ce , name , & value_zv , ast -> attr , doc_comment ) != SUCCESS ) {
51985217 zend_error_noreturn (E_COMPILE_ERROR , "Cannot redefine class constant %s::%s" ,
51995218 ZSTR_VAL (ce -> name ), ZSTR_VAL (name ));
52005219 }
5201-
5202- if (Z_CONSTANT (value_zv )) {
5203- ce -> ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED ;
5204- }
52055220 }
52065221}
52075222/* }}} */
0 commit comments