Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Zend/tests/gh21687.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-21687 (array_walk corrupts readonly and enum properties by wrapping them in IS_REFERENCE)
--FILE--
<?php
enum Foo: int {
case Bar = 0;
}

$bar = Foo::Bar;
try {
array_walk($bar, function($v) {});
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($bar);

class MyObj {
public function __construct(public readonly string $name = "test") {}
}
$obj = new MyObj();
try {
array_walk($obj, function(&$v) { $v = "modified"; });
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
echo $obj->name . "\n";
?>
--EXPECT--
Cannot acquire reference to readonly property Foo::$name
enum(Foo::Bar)
Cannot acquire reference to readonly property MyObj::$name
test
15 changes: 12 additions & 3 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,10 +1409,19 @@ static zend_result php_array_walk(
/* Add type source for property references. */
if (Z_TYPE_P(zv) != IS_REFERENCE && Z_TYPE_P(array) == IS_OBJECT) {
zend_property_info *prop_info =
zend_get_typed_property_info_for_slot(Z_OBJ_P(array), zv);
zend_get_property_info_for_slot(Z_OBJ_P(array), zv);
if (prop_info) {
ZVAL_NEW_REF(zv, zv);
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(zv), prop_info);
if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)) {
zend_throw_error(NULL,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the canonical message is Cannot indirectly modify readonly property Foo::$prop

E.g. from:

<?php

class Foo {
    public readonly int $prop;
    public function __construct() {
        $this->prop = 1;
    }
}

$foo = new Foo;
$ref =& $foo->prop;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach by-ref uses "Cannot acquire reference to readonly property", direct $ref =& $foo->prop uses "Cannot indirectly modify readonly property."

 class Foo {
      public readonly int $prop;
      public function __construct() { $this->prop = 1; }
  }
  $foo = new Foo;
  foreach ($foo as &$v) {}
  // Error: Cannot acquire reference to readonly property Foo::$prop

I matched the foreach wording since array_walk iterates properties and both are array related, but "Cannot indirectly modify" works too. @ndossche I can update the message, if you prefer, but there is a bit of inconsistency here

"Cannot acquire reference to readonly property %s::$%s",
ZSTR_VAL(prop_info->ce->name),
zend_get_unmangled_property_name(prop_info->name));
break;
}
if (ZEND_TYPE_IS_SET(prop_info->type)) {
ZVAL_NEW_REF(zv, zv);
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(zv), prop_info);
}
}
}
}
Expand Down
Loading