Skip to content

Commit 8834cf0

Browse files
committed
Handle missing class when evaluating CONST_ENUM_INIT
When resolving constants on a dynamically declared class during preloading, the enum class may not be available. Fail gracefully in that case. Possibly we shouldn't be trying to evaluate constants on non-linked classes at all?
1 parent 6aa0873 commit 8834cf0

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Zend/zend_ast.c

+7
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,13 @@ ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast
782782
: NULL;
783783

784784
zend_class_entry *ce = zend_lookup_class(class_name);
785+
if (!ce) {
786+
/* Class may not be available when resolving constants on a dynamically
787+
* declared enum during preloading. */
788+
ZEND_ASSERT(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
789+
return FAILURE;
790+
}
791+
785792
zend_enum_new(result, ce, case_name, case_value_zv);
786793
break;
787794
}

ext/opcache/tests/preload_enum.inc

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ class Test {
1010
public $x = MyEnum::Bar;
1111
}
1212
new Test;
13+
14+
if (false) {
15+
enum MyEnum2 {
16+
case Foo;
17+
}
18+
}

ext/opcache/tests/preload_enum.phpt

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ var_dump(MyEnum::Foo);
1717
var_dump(MyEnum::Bar);
1818

1919
?>
20-
--EXPECT--
20+
--EXPECTF--
2121
enum(MyEnum::Bar)
22+
23+
Warning: Can't preload unlinked class MyEnum2: Unknown reason in %s on line %d
2224
enum(MyEnum::Foo)
2325
enum(MyEnum::Bar)

0 commit comments

Comments
 (0)