Skip to content

Commit e053ba0

Browse files
authored
Do not allow side-effects when readonly property modification fails (#10757)
1 parent 792400b commit e053ba0

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Test that there can be no side-effect when a readonly property modification fails
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function __construct(
8+
public readonly string $bar
9+
) {}
10+
11+
public function write() {
12+
$this->bar = new S();
13+
}
14+
}
15+
16+
class S {
17+
public function __toString() {
18+
var_dump("Side-effect in __toString()");
19+
return "";
20+
}
21+
}
22+
23+
$foo = new Foo("");
24+
25+
try {
26+
$foo->write();
27+
} catch (Error $e) {
28+
echo $e->getMessage() . "\n";
29+
}
30+
31+
?>
32+
--EXPECT--
33+
Cannot modify readonly property Foo::$bar

Zend/zend_execute.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,11 @@ static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *inf
10091009
{
10101010
zval tmp;
10111011

1012+
if (UNEXPECTED((info->flags & ZEND_ACC_READONLY) && !(Z_PROP_FLAG_P(property_val) & IS_PROP_REINITABLE))) {
1013+
zend_readonly_property_modification_error(info);
1014+
return &EG(uninitialized_zval);
1015+
}
1016+
10121017
ZVAL_DEREF(value);
10131018
ZVAL_COPY(&tmp, value);
10141019

@@ -1017,15 +1022,7 @@ static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *inf
10171022
return &EG(uninitialized_zval);
10181023
}
10191024

1020-
if (UNEXPECTED(info->flags & ZEND_ACC_READONLY)) {
1021-
if (Z_PROP_FLAG_P(property_val) & IS_PROP_REINITABLE) {
1022-
Z_PROP_FLAG_P(property_val) &= ~IS_PROP_REINITABLE;
1023-
} else {
1024-
zval_ptr_dtor(&tmp);
1025-
zend_readonly_property_modification_error(info);
1026-
return &EG(uninitialized_zval);
1027-
}
1028-
}
1025+
Z_PROP_FLAG_P(property_val) &= ~IS_PROP_REINITABLE;
10291026

10301027
return zend_assign_to_variable(property_val, &tmp, IS_TMP_VAR, EX_USES_STRICT_TYPES());
10311028
}

Zend/zend_object_handlers.c

+8-10
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,13 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
812812
Z_TRY_ADDREF_P(value);
813813

814814
if (UNEXPECTED(prop_info)) {
815+
if (UNEXPECTED((prop_info->flags & ZEND_ACC_READONLY) && !(Z_PROP_FLAG_P(variable_ptr) & IS_PROP_REINITABLE))) {
816+
Z_TRY_DELREF_P(value);
817+
zend_readonly_property_modification_error(prop_info);
818+
variable_ptr = &EG(error_zval);
819+
goto exit;
820+
}
821+
815822
ZVAL_COPY_VALUE(&tmp, value);
816823
// Increase refcount to prevent object from being released in __toString()
817824
GC_ADDREF(zobj);
@@ -828,16 +835,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
828835
variable_ptr = &EG(error_zval);
829836
goto exit;
830837
}
831-
if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)) {
832-
if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_REINITABLE) {
833-
Z_PROP_FLAG_P(variable_ptr) &= ~IS_PROP_REINITABLE;
834-
} else {
835-
zval_ptr_dtor(&tmp);
836-
zend_readonly_property_modification_error(prop_info);
837-
variable_ptr = &EG(error_zval);
838-
goto exit;
839-
}
840-
}
838+
Z_PROP_FLAG_P(variable_ptr) &= ~IS_PROP_REINITABLE;
841839
value = &tmp;
842840
}
843841

0 commit comments

Comments
 (0)