Skip to content

Commit 7dc5bc5

Browse files
committed
Do not swap operands in array addition
As we support constant array operands nowadays, the original check didn't work anymore.
1 parent 07b3399 commit 7dc5bc5

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Array addition is not commutative -- do not swap operands
3+
--FILE--
4+
<?php
5+
6+
$array = [1, 2, 3];
7+
$array = [4, 5, 6] + $array;
8+
var_dump($array);
9+
10+
?>
11+
--EXPECT--
12+
array(3) {
13+
[0]=>
14+
int(4)
15+
[1]=>
16+
int(5)
17+
[2]=>
18+
int(6)
19+
}

ext/opcache/Optimizer/pass3.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ void zend_optimizer_pass3(zend_op_array *op_array)
106106
zend_uchar tmp_type = opline->op1_type;
107107
znode_op tmp = opline->op1;
108108

109-
if (opline->opcode != ZEND_ADD || ZEND_OP1_TYPE(opline) == IS_CONST) {
109+
if (opline->opcode != ZEND_ADD
110+
|| (ZEND_OP1_TYPE(opline) == IS_CONST
111+
&& Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_ARRAY)) {
110112
/* protection from array add: $a = array + $a is not commutative! */
111113
COPY_NODE(opline->op1, opline->op2);
112114
COPY_NODE(opline->op2, tmp);

0 commit comments

Comments
 (0)