Skip to content

Commit 39995d8

Browse files
committed
Fix the issue for access via cache slot as well
1 parent 24c2b51 commit 39995d8

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Zend/tests/readonly_props/cache_slot.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ Test interaction with cache slots
66
class Test {
77
public readonly string $prop;
88
public readonly array $prop2;
9+
public readonly object $prop3;
910
public function setProp(string $prop) {
1011
$this->prop = $prop;
1112
}
1213
public function initAndAppendProp2() {
1314
$this->prop2 = [];
1415
$this->prop2[] = 1;
1516
}
17+
public function initProp3() {
18+
$this->prop3 = new stdClass;
19+
$this->prop3->foo = 1;
20+
}
21+
public function replaceProp3() {
22+
$ref =& $this->prop3;
23+
$ref = new stdClass;
24+
}
1625
}
1726

1827
$test = new Test;
@@ -38,6 +47,22 @@ try {
3847
echo $e->getMessage(), "\n";
3948
}
4049
var_dump($test->prop2);
50+
echo "\n";
51+
52+
$test = new Test;
53+
$test->initProp3();
54+
try {
55+
$test->replaceProp3();
56+
} catch (Error $e) {
57+
echo $e->getMessage(), "\n";
58+
}
59+
var_dump($test->prop3);
60+
try {
61+
$test->replaceProp3();
62+
} catch (Error $e) {
63+
echo $e->getMessage(), "\n";
64+
}
65+
var_dump($test->prop3);
4166

4267
?>
4368
--EXPECT--
@@ -49,3 +74,12 @@ Cannot modify readonly property Test::$prop2
4974
Cannot modify readonly property Test::$prop2
5075
array(0) {
5176
}
77+
78+
object(stdClass)#3 (1) {
79+
["foo"]=>
80+
int(1)
81+
}
82+
object(stdClass)#3 (1) {
83+
["foo"]=>
84+
int(1)
85+
}

Zend/zend_execute.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,11 +2840,14 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c
28402840
ZVAL_INDIRECT(result, ptr);
28412841
zend_property_info *prop_info = CACHED_PTR_EX(cache_slot + 2);
28422842
if (prop_info) {
2843-
if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)
2844-
&& Z_TYPE_P(ptr) != IS_OBJECT) {
2843+
if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)) {
28452844
ZEND_ASSERT(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET);
2846-
zend_readonly_property_modification_error(prop_info);
2847-
ZVAL_ERROR(result);
2845+
if (Z_TYPE_P(ptr) == IS_OBJECT) {
2846+
ZVAL_COPY(result, ptr);
2847+
} else {
2848+
zend_readonly_property_modification_error(prop_info);
2849+
ZVAL_ERROR(result);
2850+
}
28482851
return;
28492852
}
28502853
if (flags) {

0 commit comments

Comments
 (0)