Skip to content

Commit e2bd92f

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Endless recursion when using + on array in foreach
2 parents 96e1d3f + 50127ce commit e2bd92f

File tree

4 files changed

+21
-59
lines changed

4 files changed

+21
-59
lines changed

Zend/tests/gh10085_1.phpt

-22
This file was deleted.

Zend/tests/gh10085_2.phpt

-25
This file was deleted.

Zend/tests/gh11171.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-11171: Test
3+
--FILE--
4+
<?php
5+
$all = ['test'];
6+
foreach ($all as &$item) {
7+
$all += [$item];
8+
}
9+
var_dump($all);
10+
?>
11+
--EXPECT--
12+
array(1) {
13+
[0]=>
14+
&string(4) "test"
15+
}

Zend/zend_operators.c

+6-12
Original file line numberDiff line numberDiff line change
@@ -1039,22 +1039,16 @@ 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+
}
10421046
if (result != op1) {
10431047
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 */
10471048
} else {
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);
1049+
SEPARATE_ARRAY(result);
10571050
}
1051+
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
10581052
}
10591053
/* }}} */
10601054

0 commit comments

Comments
 (0)