Skip to content

Don't call __set() on uninitialized typed properties (Z_EXTRA variant) #4856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Zend/tests/type_declarations/typed_properties_magic_set.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
__set() should not be invoked when setting an uninitialized typed property
--FILE--
<?php
class Test {
public int $foo;
public function __set($name, $value) {
echo "__set ", $name, " = ", $value, "\n";
}
}
$test = new Test;
$test->foo = 42;
var_dump($test->foo);
// __set will be called after unset()
unset($test->foo);
$test->foo = 42;
// __set will be called after unset() without prior initialization
$test = new Test;
unset($test->foo);
$test->foo = 42;
?>
--EXPECT--
int(42)
__set foo = 42
__set foo = 42
9 changes: 6 additions & 3 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,13 +1263,13 @@ static zend_always_inline void _object_properties_init(zend_object *object, zend

if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
do {
ZVAL_COPY_OR_DUP(dst, src);
ZVAL_COPY_OR_DUP_PROP(dst, src);
src++;
dst++;
} while (src != end);
} else {
do {
ZVAL_COPY(dst, src);
ZVAL_COPY_PROP(dst, src);
src++;
dst++;
} while (src != end);
Expand Down Expand Up @@ -3725,6 +3725,7 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name
}
}
} else {
zval *property_default_ptr;
if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
(property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
property_info->offset = property_info_ptr->offset;
Expand All @@ -3745,7 +3746,9 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name
ce->properties_info_table[ce->default_properties_count - 1] = property_info;
}
}
ZVAL_COPY_VALUE(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)], property);
property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
ZVAL_COPY_VALUE(property_default_ptr, property);
Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
}
if (ce->type & ZEND_INTERNAL_CLASS) {
switch(Z_TYPE_P(property)) {
Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
do {
dst--;
src--;
ZVAL_COPY_VALUE(dst, src);
ZVAL_COPY_VALUE_PROP(dst, src);
} while (dst != end);
pefree(src, ce->type == ZEND_INTERNAL_CLASS);
end = ce->default_properties_table;
Expand All @@ -1182,7 +1182,7 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
do {
dst--;
src--;
ZVAL_COPY_OR_DUP(dst, src);
ZVAL_COPY_OR_DUP_PROP(dst, src);
if (Z_OPT_TYPE_P(dst) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
}
Expand All @@ -1192,7 +1192,7 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
do {
dst--;
src--;
ZVAL_COPY(dst, src);
ZVAL_COPY_PROP(dst, src);
if (Z_OPT_TYPE_P(dst) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
}
Expand Down
7 changes: 7 additions & 0 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,11 @@ ZEND_API zval *zend_std_write_property(zval *object, zval *member, zval *value,
zend_assign_to_variable(variable_ptr, value, IS_TMP_VAR, EG(current_execute_data) && ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)));
goto exit;
}
if (Z_PROP_FLAG_P(variable_ptr) == IS_PROP_UNINIT) {
/* Writes to uninitializde typed properties bypass __set(). */
Z_PROP_FLAG_P(variable_ptr) = 0;
goto write_std_property;
}
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
if (EXPECTED(zobj->properties != NULL)) {
if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
Expand Down Expand Up @@ -1113,6 +1118,8 @@ ZEND_API void zend_std_unset_property(zval *object, zval *member, void **cache_s
}
goto exit;
}
/* Reset the IS_PROP_UNINIT flag, if it exists. */
Z_PROP_FLAG_P(slot) = 0;
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))
&& EXPECTED(zobj->properties != NULL)) {
if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,

do {
i_zval_ptr_dtor(dst);
ZVAL_COPY_VALUE(dst, src);
ZVAL_COPY_VALUE_PROP(dst, src);
zval_add_ref(dst);
if (UNEXPECTED(Z_ISREF_P(dst)) &&
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
Expand Down
14 changes: 14 additions & 0 deletions Zend/zend_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1262,4 +1262,18 @@ static zend_always_inline uint32_t zval_delref_p(zval* pz) {
} \
} while (0)

/* Properties store a flag distinguishing unset and unintialized properties
* (both use IS_UNDEF type) in the Z_EXTRA space. As such we also need to copy
* the Z_EXTRA space when copying property default values etc. We define separate
* macros for this purpose, so this workaround is easier to remove in the future. */
#define IS_PROP_UNINIT 1
#define Z_PROP_FLAG_P(z) Z_EXTRA_P(z)
#define ZVAL_COPY_VALUE_PROP(z, v) \
do { *(z) = *(v); } while (0)
#define ZVAL_COPY_PROP(z, v) \
do { ZVAL_COPY(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0)
#define ZVAL_COPY_OR_DUP_PROP(z, v) \
do { ZVAL_COPY_OR_DUP(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0)


#endif /* ZEND_TYPES_H */
2 changes: 1 addition & 1 deletion ext/opcache/zend_accelerator_util_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static void zend_class_copy_ctor(zend_class_entry **pce)
end = src + ce->default_properties_count;
ce->default_properties_table = dst;
for (; src != end; src++, dst++) {
ZVAL_COPY_VALUE(dst, src);
ZVAL_COPY_VALUE_PROP(dst, src);
}
}

Expand Down