Skip to content

Commit 50127ce

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Endless recursion when using + on array in foreach
2 parents 9f15193 + dc20cd9 commit 50127ce

File tree

5 files changed

+21
-61
lines changed

5 files changed

+21
-61
lines changed

NEWS

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ PHP NEWS
2222
. Fix inconsistent float negation in constant expressions. (ilutov)
2323
. Fixed bug GH-8841 (php-cli core dump calling a badly formed function).
2424
(nielsdos)
25-
. Fixed bug GH-10085 (Assertion when adding two arrays with += where the first
26-
array is contained in the second). (ilutov)
2725
. Fixed bug GH-10737 (PHP 8.1.16 segfaults on line 597 of
2826
sapi/apache2handler/sapi_apache2.c). (nielsdos, ElliotNB)
2927
. Fixed bug GH-11028 (Heap Buffer Overflow in zval_undefined_cv.). (nielsdos)

Zend/tests/gh10085_1.phpt

Lines changed: 0 additions & 22 deletions
This file was deleted.

Zend/tests/gh10085_2.phpt

Lines changed: 0 additions & 25 deletions
This file was deleted.

Zend/tests/gh11171.phpt

Lines changed: 15 additions & 0 deletions
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

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

10161016
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
10171017
{
1018+
if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1019+
/* $a += $a */
1020+
return;
1021+
}
10181022
if (result != op1) {
10191023
ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1020-
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1021-
} else if (Z_ARR_P(op1) == Z_ARR_P(op2)) {
1022-
/* $a += $a */
10231024
} else {
1024-
/* We have to duplicate op1 (even with refcount == 1) because it may be an element of op2
1025-
* and therefore its reference counter may be increased by zend_hash_merge(). That leads to
1026-
* an assertion in _zend_hash_add_or_update_i() that only allows adding elements to hash
1027-
* tables with RC1. See GH-10085 and Zend/tests/gh10085*.phpt */
1028-
zval tmp;
1029-
ZVAL_ARR(&tmp, zend_array_dup(Z_ARR_P(op1)));
1030-
zend_hash_merge(Z_ARRVAL(tmp), Z_ARRVAL_P(op2), zval_add_ref, 0);
1031-
zval_ptr_dtor(result);
1032-
ZVAL_COPY_VALUE(result, &tmp);
1025+
SEPARATE_ARRAY(result);
10331026
}
1027+
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
10341028
}
10351029
/* }}} */
10361030

0 commit comments

Comments
 (0)