Skip to content

Fix GH-16053: array_merge_recursive(): convert_to_array() may need separation #16061

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

Merged
merged 1 commit into from
Oct 2, 2024
Merged
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
3 changes: 2 additions & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3700,7 +3700,6 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
}

ZEND_ASSERT(!Z_ISREF_P(dest_entry) || Z_REFCOUNT_P(dest_entry) > 1);
SEPARATE_ZVAL(dest_entry);
Copy link
Member Author

Choose a reason for hiding this comment

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

We can remove this one (move it after convert_to_array) because convert_to_array will change dest_entry itself, not its value.

This function needs some cleanup, as there are leftovers from previous refactorings. For instance I believe that src_entry == dest_entry above is always false, and we don't use the result of ZVAL_DEREF(dest_zval) because of dest_zval = dest_entry below. However this is out of scope of this bug fix.

dest_zval = dest_entry;

if (Z_TYPE_P(dest_zval) == IS_NULL) {
Expand All @@ -3709,6 +3708,8 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
} else {
convert_to_array(dest_zval);
}
SEPARATE_ZVAL(dest_zval);

ZVAL_UNDEF(&tmp);
if (Z_TYPE_P(src_zval) == IS_OBJECT) {
ZVAL_COPY(&tmp, src_zval);
Expand Down
28 changes: 28 additions & 0 deletions ext/standard/tests/array/gh16053.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
GH-16053: Assertion failure in Zend/zend_hash.c
--FILE--
<?php

class test
{
}
$x = new test;
$arr1 = array("string" => $x);
$arr2 = array("string" => "hello");
var_dump($arr1);
var_dump(array_merge_recursive($arr1, $arr2));

?>
--EXPECTF--
array(1) {
["string"]=>
object(test)#%d (0) {
}
}
array(1) {
["string"]=>
array(1) {
[0]=>
string(5) "hello"
}
}
Loading