Skip to content

Fix refcount of declared properties when casting objects to arrays #8737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 29 additions & 0 deletions Zend/tests/bugGH-8655.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Bug GH-8655 (zval reference is not released when targetting a declared property)
--FILE--
<?php
class Foo
{
public $foo;
}

function hydrate($properties, $object)
{
foreach ($properties as $name => &$value) {
$object->$name = &$value;
}
};

$object = new Foo;

hydrate(['foo' => 123], $object);

$arrayCast = (array) $object;

$object->foo = 234;
var_dump(ReflectionReference::fromArrayElement($arrayCast, 'foo'));
echo $arrayCast['foo'];
?>
--EXPECT--
NULL
123
4 changes: 4 additions & 0 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /*
continue;
}

if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
prop = Z_REFVAL_P(prop);
}

Z_TRY_ADDREF_P(prop);
_zend_hash_append(ht, prop_info->name, prop);
}
Expand Down