Use ZEND_TYPE_IS_SET() when checking for property types

Instead of a simple if or 0 comparison. This would no longer work
if zend_type is a struct.
This commit is contained in:
Nikita Popov 2019-09-23 16:53:54 +02:00
parent 3a104c3c37
commit 6276dd826b
9 changed files with 34 additions and 29 deletions

View File

@ -1073,7 +1073,7 @@ ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
}
if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
if (prop_info->type) {
if (ZEND_TYPE_IS_SET(prop_info->type)) {
zval tmp;
ZVAL_COPY(&tmp, val);
@ -1149,7 +1149,7 @@ ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properti
(property_info->flags & ZEND_ACC_STATIC) == 0) {
zval *slot = OBJ_PROP(object, property_info->offset);
if (UNEXPECTED(property_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
zval tmp;
ZVAL_COPY_VALUE(&tmp, prop);
@ -3725,7 +3725,7 @@ ZEND_API int zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, ze
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
{
return zend_declare_typed_property(ce, name, property, access_type, doc_comment, 0);
return zend_declare_typed_property(ce, name, property, access_type, doc_comment, ZEND_TYPE_ENCODE_NONE());
}
/* }}} */
@ -4024,7 +4024,7 @@ ZEND_API int zend_update_static_property_ex(zend_class_entry *scope, zend_string
ZEND_ASSERT(!Z_ISREF_P(value));
Z_TRY_ADDREF_P(value);
if (prop_info->type) {
if (ZEND_TYPE_IS_SET(prop_info->type)) {
ZVAL_COPY_VALUE(&tmp, value);
if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
Z_TRY_DELREF_P(value);

View File

@ -2542,14 +2542,14 @@ static zend_always_inline zend_bool promotes_to_object(zval *val) {
}
static zend_always_inline zend_bool check_type_array_assignable(zend_type type) {
if (!type) {
if (!ZEND_TYPE_IS_SET(type)) {
return 1;
}
return ZEND_TYPE_IS_MASK(type) && (ZEND_TYPE_MASK(type) & (MAY_BE_ITERABLE|MAY_BE_ARRAY));
}
static zend_always_inline zend_bool check_type_stdClass_assignable(zend_type type) {
if (!type) {
if (!ZEND_TYPE_IS_SET(type)) {
return 1;
}
if (ZEND_TYPE_IS_CLASS(type)) {
@ -2928,7 +2928,8 @@ static zend_always_inline int zend_fetch_static_property_address(zval **retval,
property_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
if ((fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW)
&& UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF) && UNEXPECTED(property_info->type != 0)) {
&& UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF)
&& UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
ZSTR_VAL(property_info->ce->name),
zend_get_unmangled_property_name(property_info->name));
@ -2941,7 +2942,7 @@ static zend_always_inline int zend_fetch_static_property_address(zval **retval,
}
}
if (flags && property_info->type) {
if (flags && ZEND_TYPE_IS_SET(property_info->type)) {
zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
}

View File

@ -454,7 +454,7 @@ found:
}
offset = property_info->offset;
if (EXPECTED(!property_info->type)) {
if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) {
property_info = NULL;
} else {
*info_ptr = property_info;
@ -1482,7 +1482,7 @@ undeclared_property:
ZVAL_DEINDIRECT(ret);
if (UNEXPECTED((type == BP_VAR_R || type == BP_VAR_RW)
&& Z_TYPE_P(ret) == IS_UNDEF && property_info->type != 0)) {
&& Z_TYPE_P(ret) == IS_UNDEF && ZEND_TYPE_IS_SET(property_info->type))) {
zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
ZSTR_VAL(property_info->ce->name),
zend_get_unmangled_property_name(property_name));

View File

@ -63,7 +63,7 @@ ZEND_API void zend_object_std_dtor(zend_object *object)
if (UNEXPECTED(Z_ISREF_P(p)) &&
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
zend_property_info *prop_info = zend_get_property_info_for_slot(object, p);
if (prop_info->type) {
if (ZEND_TYPE_IS_SET(prop_info->type)) {
ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
}
}
@ -214,7 +214,7 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
if (UNEXPECTED(Z_ISREF_P(dst)) &&
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
zend_property_info *prop_info = zend_get_property_info_for_slot(new_object, dst);
if (prop_info->type) {
if (ZEND_TYPE_IS_SET(prop_info->type)) {
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
}
}

View File

@ -107,7 +107,7 @@ static inline zend_property_info *zend_get_property_info_for_slot(zend_object *o
static inline zend_property_info *zend_get_typed_property_info_for_slot(zend_object *obj, zval *slot)
{
zend_property_info *prop_info = zend_get_property_info_for_slot(obj, slot);
if (prop_info && prop_info->type) {
if (prop_info && ZEND_TYPE_IS_SET(prop_info->type)) {
return prop_info;
}
return NULL;

View File

@ -1170,7 +1170,7 @@ ZEND_VM_HANDLER(29, ZEND_ASSIGN_STATIC_PROP_OP, ANY, ANY, OP)
prop = Z_REFVAL_P(prop);
}
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
/* special case for typed properties */
zend_binary_assign_op_typed_prop(prop_info, prop, value OPLINE_CC EXECUTE_DATA_CC);
} else {
@ -1481,7 +1481,8 @@ ZEND_VM_HANDLER(38, ZEND_PRE_INC_STATIC_PROP, ANY, ANY, CACHE_SLOT)
HANDLE_EXCEPTION();
}
zend_pre_incdec_property_zval(prop, prop_info->type ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
zend_pre_incdec_property_zval(prop,
ZEND_TYPE_IS_SET(prop_info->type) ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}
@ -1506,7 +1507,8 @@ ZEND_VM_HANDLER(40, ZEND_POST_INC_STATIC_PROP, ANY, ANY, CACHE_SLOT)
HANDLE_EXCEPTION();
}
zend_post_incdec_property_zval(prop, prop_info->type ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
zend_post_incdec_property_zval(prop,
ZEND_TYPE_IS_SET(prop_info->type) ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}
@ -2612,7 +2614,7 @@ ZEND_VM_HANDLER(25, ZEND_ASSIGN_STATIC_PROP, ANY, ANY, CACHE_SLOT, SPEC(OP_DATA=
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
value = zend_assign_to_typed_prop(prop_info, prop, value EXECUTE_DATA_CC);
FREE_OP_DATA();
} else {
@ -2873,7 +2875,7 @@ ZEND_VM_HANDLER(33, ZEND_ASSIGN_STATIC_PROP_REF, ANY, ANY, CACHE_SLOT|SRC)
if (UNEXPECTED(!zend_wrong_assign_to_variable_reference(prop, value_ptr OPLINE_CC EXECUTE_DATA_CC))) {
prop = &EG(uninitialized_zval);
}
} else if (UNEXPECTED(prop_info->type)) {
} else if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
prop = zend_assign_to_typed_property_reference(prop_info, prop, value_ptr EXECUTE_DATA_CC);
} else {
zend_assign_to_variable_reference(prop, value_ptr);

View File

@ -822,7 +822,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_STATIC_PROP_OP_SPEC_HAN
prop = Z_REFVAL_P(prop);
}
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
/* special case for typed properties */
zend_binary_assign_op_typed_prop(prop_info, prop, value OPLINE_CC EXECUTE_DATA_CC);
} else {
@ -852,7 +852,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_PRE_INC_STATIC_PROP_SPEC_HANDL
HANDLE_EXCEPTION();
}
zend_pre_incdec_property_zval(prop, prop_info->type ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
zend_pre_incdec_property_zval(prop,
ZEND_TYPE_IS_SET(prop_info->type) ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}
@ -871,7 +872,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_POST_INC_STATIC_PROP_SPEC_HAND
HANDLE_EXCEPTION();
}
zend_post_incdec_property_zval(prop, prop_info->type ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
zend_post_incdec_property_zval(prop,
ZEND_TYPE_IS_SET(prop_info->type) ? prop_info : NULL OPLINE_CC EXECUTE_DATA_CC);
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}
@ -976,7 +978,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_STATIC_PROP_SPEC_OP_DAT
value = RT_CONSTANT((opline+1), (opline+1)->op1);
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
value = zend_assign_to_typed_prop(prop_info, prop, value EXECUTE_DATA_CC);
} else {
@ -1007,7 +1009,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_STATIC_PROP_SPEC_OP_DAT
value = _get_zval_ptr_tmp((opline+1)->op1.var EXECUTE_DATA_CC);
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
value = zend_assign_to_typed_prop(prop_info, prop, value EXECUTE_DATA_CC);
zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var));
} else {
@ -1038,7 +1040,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_STATIC_PROP_SPEC_OP_DAT
value = _get_zval_ptr_var((opline+1)->op1.var EXECUTE_DATA_CC);
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
value = zend_assign_to_typed_prop(prop_info, prop, value EXECUTE_DATA_CC);
zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var));
} else {
@ -1069,7 +1071,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_STATIC_PROP_SPEC_OP_DAT
value = _get_zval_ptr_cv_BP_VAR_R((opline+1)->op1.var EXECUTE_DATA_CC);
if (UNEXPECTED(prop_info->type)) {
if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
value = zend_assign_to_typed_prop(prop_info, prop, value EXECUTE_DATA_CC);
} else {
@ -1106,7 +1108,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_STATIC_PROP_REF_SPEC_HA
if (UNEXPECTED(!zend_wrong_assign_to_variable_reference(prop, value_ptr OPLINE_CC EXECUTE_DATA_CC))) {
prop = &EG(uninitialized_zval);
}
} else if (UNEXPECTED(prop_info->type)) {
} else if (UNEXPECTED(ZEND_TYPE_IS_SET(prop_info->type))) {
prop = zend_assign_to_typed_property_reference(prop_info, prop, value_ptr EXECUTE_DATA_CC);
} else {
zend_assign_to_variable_reference(prop, value_ptr);

View File

@ -3703,7 +3703,7 @@ static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value
} else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
}
if (!prop || (prop_info->type && Z_ISUNDEF_P(prop))) {
if (!prop || (ZEND_TYPE_IS_SET(prop_info->type) && Z_ISUNDEF_P(prop))) {
continue;
}
@ -3815,7 +3815,7 @@ ZEND_METHOD(reflection_class, setStaticPropertyValue)
}
}
if (prop_info->type && !zend_verify_property_type(prop_info, value, 0)) {
if (ZEND_TYPE_IS_SET(prop_info->type) && !zend_verify_property_type(prop_info, value, 0)) {
return;
}

View File

@ -76,7 +76,7 @@ static void php_object_property_dump(zend_property_info *prop_info, zval *zv, ze
}
if (Z_TYPE_P(zv) == IS_UNDEF) {
ZEND_ASSERT(prop_info->type);
ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type));
zend_string *type_str = zend_type_to_string(prop_info->type);
php_printf("%*cuninitialized(%s)\n",
level + 1, ' ', ZSTR_VAL(type_str));