Skip to content

Commit 96e3a9d

Browse files
nicolas-grekasbwoebi
authored andcommitted
Fix RC=1 references of declared properties when casting objects to arrays
Fixes GH-8655. Closes GH-8737.
1 parent f768f3d commit 96e3a9d

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
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 leak in Enum::from/tryFrom for internal enums when using JIT (ilutov)
88
. Fixed calling internal methods with a static return type from
99
extension code. (Sara)
10+
. Fixed bug GH-8655 (Casting an object to array does not unwrap refcount=1
11+
references). (Nicolas Grekas)
1012

1113
- Date:
1214
. Fixed bug #72963 (Null-byte injection in CreateFromFormat and related

Zend/tests/bugGH-8655.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug GH-8655 (zval reference is not released when targetting a declared property)
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
public $foo;
8+
}
9+
10+
function hydrate($properties, $object)
11+
{
12+
foreach ($properties as $name => &$value) {
13+
$object->$name = &$value;
14+
}
15+
};
16+
17+
$object = new Foo;
18+
19+
hydrate(['foo' => 123], $object);
20+
21+
$arrayCast = (array) $object;
22+
23+
$object->foo = 234;
24+
var_dump(ReflectionReference::fromArrayElement($arrayCast, 'foo'));
25+
echo $arrayCast['foo'];
26+
?>
27+
--EXPECT--
28+
NULL
29+
123

Zend/zend_object_handlers.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /*
111111
continue;
112112
}
113113

114+
if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
115+
prop = Z_REFVAL_P(prop);
116+
}
117+
114118
Z_TRY_ADDREF_P(prop);
115119
_zend_hash_append(ht, prop_info->name, prop);
116120
}

0 commit comments

Comments
 (0)