Skip to content

Commit 1216830

Browse files
committed
Fix array_merge_recursive(): convert_to_array() may need separation
1 parent c639614 commit 1216830

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

ext/standard/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,13 +3694,11 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
36943694
ZVAL_DEREF(src_zval);
36953695
ZVAL_DEREF(dest_zval);
36963696
thash = Z_TYPE_P(dest_zval) == IS_ARRAY ? Z_ARRVAL_P(dest_zval) : NULL;
3697-
if ((thash && GC_IS_RECURSIVE(thash)) || (src_entry == dest_entry && Z_ISREF_P(dest_entry) && (Z_REFCOUNT_P(dest_entry) % 2))) {
3697+
if (thash && GC_IS_RECURSIVE(thash)) {
36983698
zend_throw_error(NULL, "Recursion detected");
36993699
return 0;
37003700
}
37013701

3702-
ZEND_ASSERT(!Z_ISREF_P(dest_entry) || Z_REFCOUNT_P(dest_entry) > 1);
3703-
SEPARATE_ZVAL(dest_entry);
37043702
dest_zval = dest_entry;
37053703

37063704
if (Z_TYPE_P(dest_zval) == IS_NULL) {
@@ -3709,6 +3707,8 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
37093707
} else {
37103708
convert_to_array(dest_zval);
37113709
}
3710+
SEPARATE_ZVAL(dest_zval);
3711+
37123712
ZVAL_UNDEF(&tmp);
37133713
if (Z_TYPE_P(src_zval) == IS_OBJECT) {
37143714
ZVAL_COPY(&tmp, src_zval);

ext/standard/tests/array/gh16053.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GH-16053: Assertion failure in Zend/zend_hash.c
3+
--FILE--
4+
<?php
5+
6+
class test
7+
{
8+
}
9+
$x = new test;
10+
$arr1 = array("string" => $x);
11+
$arr2 = array("string" => "hello");
12+
var_dump($arr1);
13+
var_dump(array_merge_recursive($arr1, $arr2));
14+
15+
?>
16+
--EXPECTF--
17+
array(1) {
18+
["string"]=>
19+
object(test)#%d (0) {
20+
}
21+
}
22+
array(1) {
23+
["string"]=>
24+
array(1) {
25+
[0]=>
26+
string(5) "hello"
27+
}
28+
}

0 commit comments

Comments
 (0)