Skip to content

Commit 8f7ed89

Browse files
committed
zend_objects: Disallow clone_with by reference
1 parent e76dbde commit 8f7ed89

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Zend/tests/clone/clone_with_013.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Clone with references
3+
--FILE--
4+
<?php
5+
6+
$x = new stdClass();
7+
8+
try {
9+
$ref = 'reference';
10+
var_dump(clone($x, ['x' => &$ref]));
11+
} catch (Throwable $e) {
12+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
13+
}
14+
15+
?>
16+
--EXPECT--
17+
Error: Cannot assign by reference when cloning with updated properties

Zend/zend_objects.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const
297297
zend_string *key;
298298
zval *val;
299299
ZEND_HASH_FOREACH_KEY_VAL(properties, num_key, key, val) {
300+
if (UNEXPECTED(Z_TYPE_P(val) == IS_REFERENCE)) {
301+
zend_throw_error(NULL, "Cannot assign by reference when cloning with updated properties");
302+
break;
303+
}
304+
300305
if (UNEXPECTED(key == NULL)) {
301306
key = zend_long_to_str(num_key);
302307
new_object->handlers->write_property(new_object, key, val, NULL);

0 commit comments

Comments
 (0)