Skip to content

Commit a99d08b

Browse files
committed
Fix type inference for FE_RESET on invalid type
FE_RESET sets the result to UNDEF in this case. We should infer some type here, because no type generally implies unreachable code. In this case SCCP ended up replacing the FE_RESET result with null, including in FE_FREE.
1 parent c088a93 commit a99d08b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Zend/tests/foreach_over_null.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
foreach over null
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
foreach (null as $v) {
8+
echo "Foo\n";
9+
}
10+
}
11+
test();
12+
13+
?>
14+
--EXPECTF--
15+
Warning: foreach() argument must be of type array|object, null given in %s on line %d

ext/opcache/Optimizer/zend_inference.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,10 @@ static zend_always_inline int _zend_update_type_info(
31823182
} else {
31833183
tmp = MAY_BE_RC1 | MAY_BE_RCN | (t1 & (MAY_BE_ARRAY | MAY_BE_OBJECT | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF));
31843184
}
3185+
/* The result is set to UNDEF for invalid foreach inputs. */
3186+
if ((t1 & (MAY_BE_ANY | MAY_BE_UNDEF)) & ~(MAY_BE_ARRAY | MAY_BE_OBJECT)) {
3187+
tmp |= MAY_BE_UNDEF;
3188+
}
31853189
UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
31863190
COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->result_def);
31873191
break;

0 commit comments

Comments
 (0)