Skip to content

Commit bfcf322

Browse files
committed
Fixed bug #72369 (array_merge() produces references in PHP7)
1 parent 65b6950 commit bfcf322

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PHP NEWS
3838
. Fixed bug #72197 (pg_lo_create arbitrary read). (Anatol)
3939

4040
- Standard:
41+
. Fixed bug #72369 (array_merge() produces references in PHP7). (Dmitry)
4142
. Fixed bug #72300 (ignore_user_abort(false) has no effect). (Laruence)
4243
. Fixed bug #72229 (Wrong reference when serialize/unserialize an object).
4344
(Laruence)

ext/standard/array.c

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

30973097
ZEND_HASH_FOREACH_STR_KEY_VAL(src, string_key, src_entry) {
3098-
if (string_key) {
3099-
if (Z_REFCOUNTED_P(src_entry)) {
3098+
if (Z_REFCOUNTED_P(src_entry)) {
3099+
if (UNEXPECTED(Z_ISREF_P(src_entry))
3100+
&& UNEXPECTED(Z_REFCOUNT_P(src_entry) == 1)) {
3101+
ZVAL_UNREF(src_entry);
3102+
if (Z_REFCOUNTED_P(src_entry)) {
3103+
Z_ADDREF_P(src_entry);
3104+
}
3105+
} else {
31003106
Z_ADDREF_P(src_entry);
31013107
}
3108+
}
3109+
if (string_key) {
31023110
zend_hash_update(dest, string_key, src_entry);
31033111
} else {
3104-
if (Z_REFCOUNTED_P(src_entry)) {
3105-
Z_ADDREF_P(src_entry);
3106-
}
31073112
zend_hash_next_index_insert_new(dest, src_entry);
31083113
}
31093114
} 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)