Skip to content

Commit 8ac373b

Browse files
committed
Fix the issue for access via cache slot as well
1 parent 55122c3 commit 8ac373b

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
@@ -2832,11 +2832,14 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c
28322832
ZVAL_INDIRECT(result, ptr);
28332833
zend_property_info *prop_info = CACHED_PTR_EX(cache_slot + 2);
28342834
if (prop_info) {
2835-
if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)
2836-
&& Z_TYPE_P(ptr) != IS_OBJECT) {
2835+
if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)) {
28372836
ZEND_ASSERT(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET);
2838-
zend_readonly_property_modification_error(prop_info);
2839-
ZVAL_ERROR(result);
2837+
if (Z_TYPE_P(ptr) == IS_OBJECT) {
2838+
ZVAL_COPY(result, ptr);
2839+
} else {
2840+
zend_readonly_property_modification_error(prop_info);
2841+
ZVAL_ERROR(result);
2842+
}
28402843
return;
28412844
}
28422845
if (flags) {

0 commit comments

Comments
 (0)