Skip to content

Commit 8360efd

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Fix add_function_array() assertion when op2 contains op1
2 parents 50f58c8 + c4f56c5 commit 8360efd

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

Zend/tests/gh10085_1.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-10085: Assertion in add_function_array()
3+
--FILE--
4+
<?php
5+
$i = [[], 0];
6+
$ref = &$i;
7+
$i[0] += $ref;
8+
var_dump($i);
9+
?>
10+
--EXPECT--
11+
array(2) {
12+
[0]=>
13+
array(2) {
14+
[0]=>
15+
array(0) {
16+
}
17+
[1]=>
18+
int(0)
19+
}
20+
[1]=>
21+
int(0)
22+
}

Zend/tests/gh10085_2.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-10085: Assertion in add_function_array()
3+
--FILE--
4+
<?php
5+
$tmp = [0];
6+
unset($tmp[0]);
7+
$i = [$tmp, 0];
8+
unset($tmp);
9+
$ref = &$i;
10+
$i[0] += $ref;
11+
var_dump($i);
12+
?>
13+
--EXPECT--
14+
array(2) {
15+
[0]=>
16+
array(2) {
17+
[0]=>
18+
array(0) {
19+
}
20+
[1]=>
21+
int(0)
22+
}
23+
[1]=>
24+
int(0)
25+
}

Zend/zend_operators.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,16 +1039,22 @@ static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const cha
10391039

10401040
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
10411041
{
1042-
if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1043-
/* $a += $a */
1044-
return;
1045-
}
10461042
if (result != op1) {
10471043
ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1044+
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1045+
} else if (Z_ARR_P(op1) == Z_ARR_P(op2)) {
1046+
/* $a += $a */
10481047
} else {
1049-
SEPARATE_ARRAY(result);
1048+
/* We have to duplicate op1 (even with refcount == 1) because it may be an element of op2
1049+
* and therefore its reference counter may be increased by zend_hash_merge(). That leads to
1050+
* an assertion in _zend_hash_add_or_update_i() that only allows adding elements to hash
1051+
* tables with RC1. See GH-10085 and Zend/tests/gh10085*.phpt */
1052+
zval tmp;
1053+
ZVAL_ARR(&tmp, zend_array_dup(Z_ARR_P(op1)));
1054+
zend_hash_merge(Z_ARRVAL(tmp), Z_ARRVAL_P(op2), zval_add_ref, 0);
1055+
zval_ptr_dtor(result);
1056+
ZVAL_COPY_VALUE(result, &tmp);
10501057
}
1051-
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
10521058
}
10531059
/* }}} */
10541060

0 commit comments

Comments
 (0)