Do not swap operands in array addition

As we support constant array operands nowadays, the original check
didn't work anymore.
This commit is contained in:
Nikita Popov 2015-12-14 00:28:41 +01:00
parent 07b33992a2
commit 7dc5bc5063
2 changed files with 22 additions and 1 deletions

View file

@ -0,0 +1,19 @@
--TEST--
Array addition is not commutative -- do not swap operands
--FILE--
<?php
$array = [1, 2, 3];
$array = [4, 5, 6] + $array;
var_dump($array);
?>
--EXPECT--
array(3) {
[0]=>
int(4)
[1]=>
int(5)
[2]=>
int(6)
}

View file

@ -106,7 +106,9 @@ void zend_optimizer_pass3(zend_op_array *op_array)
zend_uchar tmp_type = opline->op1_type;
znode_op tmp = opline->op1;
if (opline->opcode != ZEND_ADD || ZEND_OP1_TYPE(opline) == IS_CONST) {
if (opline->opcode != ZEND_ADD
|| (ZEND_OP1_TYPE(opline) == IS_CONST
&& Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_ARRAY)) {
/* protection from array add: $a = array + $a is not commutative! */
COPY_NODE(opline->op1, opline->op2);
COPY_NODE(opline->op2, tmp);