Skip to content

Commit

Permalink
Fix phpGH-10914: OPCache with Enum and Callback functions results in …
Browse files Browse the repository at this point in the history
…segmentation fault

See linked issue for analysis.
  • Loading branch information
nielsdos committed Jul 11, 2023
1 parent bbe72f1 commit e6abc93
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
26 changes: 26 additions & 0 deletions ext/opcache/tests/gh10914.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-10914 (OPCache with Enum and Callback functions results in segmentation fault)
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.protect_memory=1
opcache.preload={PWD}/preload_gh10914.inc
--EXTENSIONS--
opcache
--SKIPIF--
<?php
if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows');
?>
--FILE--
<?php
$x = new ReflectionEnum(ExampleEnum::class);
var_dump($x->getCases()[0]->getValue());
var_dump($x->getCases()[0]->getBackingValue());
var_dump($x->getCase('FIRST')->getValue());
var_dump($x->getCase('FIRST')->getBackingValue());
?>
--EXPECT--
enum(ExampleEnum::FIRST)
string(4) "AAAb"
enum(ExampleEnum::FIRST)
string(4) "AAAb"
5 changes: 5 additions & 0 deletions ext/opcache/tests/preload_gh10914.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
enum ExampleEnum: string
{
case FIRST = "AAA"."b";
}
4 changes: 2 additions & 2 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6798,7 +6798,7 @@ ZEND_METHOD(ReflectionEnum, getCase)

GET_REFLECTION_OBJECT_PTR(ce);

zend_class_constant *constant = zend_hash_find_ptr(&ce->constants_table, name);
zend_class_constant *constant = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), name);
if (constant == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Case %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
RETURN_THROWS();
Expand All @@ -6825,7 +6825,7 @@ ZEND_METHOD(ReflectionEnum, getCases)
GET_REFLECTION_OBJECT_PTR(ce);

array_init(return_value);
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
ZEND_HASH_FOREACH_STR_KEY_PTR(CE_CONSTANTS_TABLE(ce), name, constant) {
if (ZEND_CLASS_CONST_FLAGS(constant) & ZEND_CLASS_CONST_IS_CASE) {
zval class_const;
reflection_enum_case_factory(ce, name, constant, &class_const);
Expand Down

0 comments on commit e6abc93

Please sign in to comment.