Skip to content

Commit

Permalink
Fix ASSIGN_DIM result inference with typed refs
Browse files Browse the repository at this point in the history
Same issue as with ASSIGN. Also make the handling for ASSIGN more
precise, we can only have conversions between scalar values.
  • Loading branch information
nikic committed Sep 28, 2021
1 parent cdc05eb commit 1bb7ee3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Zend/tests/assign_typed_ref_result.phpt
Expand Up @@ -11,8 +11,16 @@ function test() {
$ref =& $obj->prop;
var_dump($ref = 0);
}
function test2() {
$obj = new Test;
$ary = [];
$ary[0] =& $obj->prop;
var_dump($ary[0] = 0);
}
test();
test2();

?>
--EXPECT--
string(1) "0"
string(1) "0"
10 changes: 7 additions & 3 deletions ext/opcache/Optimizer/zend_inference.c
Expand Up @@ -2757,6 +2757,10 @@ static zend_always_inline int _zend_update_type_info(
if (OP1_DATA_INFO() & MAY_BE_UNDEF) {
tmp |= MAY_BE_NULL;
}
if (t1 & MAY_BE_ARRAY_OF_REF) {
/* A scalar type conversion may occur when assigning to a typed reference. */
tmp |= MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING;
}
}
if (t1 & MAY_BE_OBJECT) {
tmp |= MAY_BE_REF;
Expand Down Expand Up @@ -2866,9 +2870,9 @@ static zend_always_inline int _zend_update_type_info(
}
if (ssa_op->result_def >= 0) {
if (tmp & MAY_BE_REF) {
/* Assignment to typed reference may change type.
* Be conservative and don't assume anything. */
tmp = MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
/* A scalar type conversion may occur when assigning to a typed reference. */
tmp &= ~MAY_BE_REF;
tmp |= MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_RC1|MAY_BE_RCN;
}
UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->result_def);
Expand Down

0 comments on commit 1bb7ee3

Please sign in to comment.