Skip to content

Commit 4796183

Browse files
committed
Add missing scope check for readonly prop initialization
If the initializing assignment is an array append we will go through the UNDEF codepath of get_property_ptr_ptr, which did not verify that the initialization scope is valid.
1 parent 5a1ab2c commit 4796183

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Use array append as initialization
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
public readonly array $a;
8+
9+
public function init() {
10+
$this->a[] = 1;
11+
var_dump($this->a);
12+
}
13+
}
14+
15+
16+
function init() {
17+
$c = new C;
18+
$c->a[] = 1;
19+
var_dump($c->a);
20+
}
21+
22+
(new C)->init();
23+
24+
try {
25+
init();
26+
} catch (Error $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
30+
?>
31+
--EXPECT--
32+
array(1) {
33+
[0]=>
34+
int(1)
35+
}
36+
Cannot initialize readonly property C::$a from global scope

Zend/zend_object_handlers.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,9 @@ ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *nam
10201020
ZVAL_NULL(retval);
10211021
zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
10221022
}
1023+
} else if (prop_info && UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)
1024+
&& !verify_readonly_initialization_access(prop_info, zobj->ce, name, "initialize")) {
1025+
retval = &EG(error_zval);
10231026
}
10241027
} else {
10251028
/* we do have getter - fail and let it try again with usual get/set */

0 commit comments

Comments
 (0)