Skip to content

Commit 999762a

Browse files
committed
Disallow getting reference of unset readonly property
Closes GH-7942
1 parent f16d35c commit 999762a

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
. Fixed freeing of internal attribute arguments. (Bob)
88
. Fixed bug GH-8070 (memory leak of internal function attribute hash).
99
(Tim Düsterhus)
10+
. Fixed bug GH-7942 (Disallow getting reference of unset readonly property).
11+
(ilutov)
1012

1113
- Intl:
1214
. Fixed bug GH-8115 (Can't catch arg type deprecation when instantiating Intl

Zend/tests/readonly_props/array_append_initialization.phpt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ function init() {
1919
var_dump($c->a);
2020
}
2121

22-
(new C)->init();
22+
try {
23+
(new C)->init();
24+
} catch (Error $e) {
25+
echo $e->getMessage(), "\n";
26+
}
2327

2428
try {
2529
init();
@@ -29,8 +33,5 @@ try {
2933

3034
?>
3135
--EXPECT--
32-
array(1) {
33-
[0]=>
34-
int(1)
35-
}
36+
Cannot modify readonly property C::$a
3637
Cannot initialize readonly property C::$a from global scope

Zend/tests/readonly_props/gh7942.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-7942: Disallow getting reference from unset readonly property
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public readonly int $bar;
8+
public function __construct(int &$bar) {
9+
$this->bar = &$bar;
10+
}
11+
}
12+
13+
try {
14+
$i = 42;
15+
new Foo($i);
16+
} catch (Error $e) {
17+
echo $e->getMessage(), PHP_EOL;
18+
}
19+
20+
?>
21+
--EXPECT--
22+
Cannot modify readonly property Foo::$bar

Zend/zend_object_handlers.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,10 @@ 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")) {
1023+
} else if (prop_info && UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)) {
1024+
if (verify_readonly_initialization_access(prop_info, zobj->ce, name, "initialize")) {
1025+
zend_readonly_property_modification_error(prop_info);
1026+
}
10251027
retval = &EG(error_zval);
10261028
}
10271029
} else {

ext/opcache/tests/jit/fetch_obj_006.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ $appendProp2 = (function() {
2121
$this->prop[] = 1;
2222
})->bindTo($test, Test::class);
2323
$appendProp2();
24-
$appendProp2();
2524
?>
2625
--EXPECTF--
2726
Fatal error: Uncaught Error: Cannot modify readonly property Test::$prop in %sfetch_obj_006.php:13
2827
Stack trace:
29-
#0 %sfetch_obj_006.php(16): Test->{closure}()
28+
#0 %sfetch_obj_006.php(15): Test->{closure}()
3029
#1 {main}
3130
thrown in %sfetch_obj_006.php on line 13

0 commit comments

Comments
 (0)