Skip to content

Commit df7dcce

Browse files
committed
Merge branch 'PHP-7.0'
* PHP-7.0: Fixed bug #72369 (array_merge() produces references in PHP7)
2 parents e7e79aa + bfcf322 commit df7dcce

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

ext/standard/array.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,15 +3099,20 @@ PHPAPI int php_array_merge(HashTable *dest, HashTable *src) /* {{{ */
30993099
zend_string *string_key;
31003100

31013101
ZEND_HASH_FOREACH_STR_KEY_VAL(src, string_key, src_entry) {
3102-
if (string_key) {
3103-
if (Z_REFCOUNTED_P(src_entry)) {
3102+
if (Z_REFCOUNTED_P(src_entry)) {
3103+
if (UNEXPECTED(Z_ISREF_P(src_entry))
3104+
&& UNEXPECTED(Z_REFCOUNT_P(src_entry) == 1)) {
3105+
ZVAL_UNREF(src_entry);
3106+
if (Z_REFCOUNTED_P(src_entry)) {
3107+
Z_ADDREF_P(src_entry);
3108+
}
3109+
} else {
31043110
Z_ADDREF_P(src_entry);
31053111
}
3112+
}
3113+
if (string_key) {
31063114
zend_hash_update(dest, string_key, src_entry);
31073115
} else {
3108-
if (Z_REFCOUNTED_P(src_entry)) {
3109-
Z_ADDREF_P(src_entry);
3110-
}
31113116
zend_hash_next_index_insert_new(dest, src_entry);
31123117
}
31133118
} ZEND_HASH_FOREACH_END();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #72369 (array_merge() produces references in PHP7)
3+
--FILE--
4+
<?php
5+
$x = 'xxx';
6+
$d = ['test' => &$x];
7+
unset($x);
8+
$a = ['test' => 'yyy'];
9+
$a = array_merge($a, $d);
10+
debug_zval_dump($a);
11+
?>
12+
--EXPECTF--
13+
array(1) refcount(%d){
14+
["test"]=>
15+
string(3) "xxx" refcount(%d)
16+
}

0 commit comments

Comments
 (0)