Skip to content

Commit 2759e6b

Browse files
committed
Fixed assertion when check "instanceof" on unlinked class
1 parent 124ac49 commit 2759e6b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Zend/Optimizer/zend_inference.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,17 @@ static zend_class_entry *join_class_entries(
36303630
return ce1;
36313631
}
36323632

3633+
static bool safe_instanceof(zend_class_entry *ce1, zend_class_entry *ce2) {
3634+
if (ce1 == ce2) {
3635+
return 1;
3636+
}
3637+
if (!(ce1->ce_flags & ZEND_ACC_LINKED)) {
3638+
/* This case could be generalized, similarly to unlinked_instanceof */
3639+
return 0;
3640+
}
3641+
return instanceof_function(ce1, ce2);
3642+
}
3643+
36333644
static zend_result zend_infer_types_ex(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_bitset worklist, zend_long optimization_level)
36343645
{
36353646
zend_basic_block *blocks = ssa->cfg.blocks;
@@ -3661,7 +3672,7 @@ static zend_result zend_infer_types_ex(const zend_op_array *op_array, const zend
36613672
if (!ce) {
36623673
ce = constraint->ce;
36633674
is_instanceof = 1;
3664-
} else if (is_instanceof && instanceof_function(constraint->ce, ce)) {
3675+
} else if (is_instanceof && safe_instanceof(constraint->ce, ce)) {
36653676
ce = constraint->ce;
36663677
} else {
36673678
/* Ignore the constraint (either ce instanceof constraint->ce or
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Type inference 003: instanceof with unlinked class
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
--FILE--
8+
<?php
9+
class C implements I {
10+
public static function wrap(\Throwable $e): C {
11+
return $e instanceof C ? $e : new C($e->getMessage());
12+
}
13+
}
14+
?>
15+
--EXPECTF--
16+
Fatal error: Uncaught Error: Interface "I" not found in %sinference_003.php:2
17+
Stack trace:
18+
#0 {main}
19+
thrown in %sinference_003.php on line 2
20+

0 commit comments

Comments
 (0)