gen_stub: fix invalid C variable name for namespaced types in union/intersection type list (#21717)

When generating a union or intersection type list with multiple class
types, the variable holding each zend_string* was declared using
toVarEscapedName() (backslashes replaced by underscores), but the
subsequent ZEND_TYPE_INIT_CLASS() reference used toEscapedName()
(backslashes escaped as \\), producing an invalid C identifier.

Backported to PHP 8.5.
This commit is contained in:
Jérôme Tamarelle authored and Máté Kocsis committed 2026-04-21 21:48:26 +02:00
1 parent cb363ec0e3
commit 9cf418231e
5 files changed
+53 -3

No files matched your search

+2 -2
View File
@@ -2511,8 +2511,8 @@ abstract class VariableLike
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->num_types = $classTypeCount;\n";
foreach ($arginfoType->classTypes as $k => $classType) {
$escapedClassName = $classType->toEscapedName();
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$escapedClassName}, 0, 0);\n";
$varEscapedClassName = $classType->toVarEscapedName();
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$varEscapedClassName}, 0, 0);\n";
}
$typeMaskCode = $this->type->toArginfoType()->toTypeMask();
+2
View File
@@ -67,6 +67,7 @@ static zend_class_entry *zend_test_forbid_dynamic_call;
static zend_class_entry *zend_test_ns_foo_class;
static zend_class_entry *zend_test_ns_unlikely_compile_error_class;
static zend_class_entry *zend_test_ns_not_unlikely_compile_error_class;
static zend_class_entry *zend_test_ns_bar_class;
static zend_class_entry *zend_test_ns2_foo_class;
static zend_class_entry *zend_test_ns2_ns_foo_class;
static zend_class_entry *zend_test_unit_enum;
@@ -1488,6 +1489,7 @@ PHP_MINIT_FUNCTION(zend_test)
zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
zend_test_ns_unlikely_compile_error_class = register_class_ZendTestNS_UnlikelyCompileError();
zend_test_ns_not_unlikely_compile_error_class = register_class_ZendTestNS_NotUnlikelyCompileError();
zend_test_ns_bar_class = register_class_ZendTestNS_Bar();
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();
+4
View File
@@ -361,6 +361,8 @@ namespace ZendTestNS {
public function method(): int {}
}
interface Bar {}
class UnlikelyCompileError {
/* This method signature would create a compile error due to the string
* "ZendTestNS\UnlikelyCompileError" in the generated macro call */
@@ -381,6 +383,8 @@ namespace ZendTestNS2 {
class Foo {
public ZendSubNS\Foo $foo;
public ZendSubNS\Foo&\ZendTestNS\Bar $intersectionProp;
public ZendSubNS\Foo|\ZendTestNS\Bar $unionProp;
public function method(): void {}
}
+37 -1
View File
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b70af6d65c3d9a7242bc885d2262e0ebfee2473f */
* Stub hash: a3b22072c3b653cba5dab9feeb6b11ed9ab7114c */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_trigger_bailout, 0, 0, IS_NEVER, 0)
ZEND_END_ARG_INFO()
@@ -1289,6 +1289,16 @@ static zend_class_entry *register_class_ZendTestNS_Foo(void)
return class_entry;
}
static zend_class_entry *register_class_ZendTestNS_Bar(void)
{
zend_class_entry ce, *class_entry;
INIT_NS_CLASS_ENTRY(ce, "ZendTestNS", "Bar", NULL);
class_entry = zend_register_internal_interface(&ce);
return class_entry;
}
static zend_class_entry *register_class_ZendTestNS_UnlikelyCompileError(void)
{
zend_class_entry ce, *class_entry;
@@ -1335,6 +1345,32 @@ static zend_class_entry *register_class_ZendTestNS2_Foo(void)
zend_declare_typed_property(class_entry, property_foo_name, &property_foo_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_CLASS(property_foo_class_ZendTestNS2_ZendSubNS_Foo, 0, 0));
zend_string_release(property_foo_name);
zval property_intersectionProp_default_value;
ZVAL_UNDEF(&property_intersectionProp_default_value);
zend_string *property_intersectionProp_name = zend_string_init("intersectionProp", sizeof("intersectionProp") - 1, 1);
zend_string *property_intersectionProp_class_ZendTestNS2_ZendSubNS_Foo = zend_string_init("ZendTestNS2\\ZendSubNS\\Foo", sizeof("ZendTestNS2\\ZendSubNS\\Foo") - 1, 1);
zend_string *property_intersectionProp_class_ZendTestNS_Bar = zend_string_init("ZendTestNS\\Bar", sizeof("ZendTestNS\\Bar") - 1, 1);
zend_type_list *property_intersectionProp_type_list = malloc(ZEND_TYPE_LIST_SIZE(2));
property_intersectionProp_type_list->num_types = 2;
property_intersectionProp_type_list->types[0] = (zend_type) ZEND_TYPE_INIT_CLASS(property_intersectionProp_class_ZendTestNS2_ZendSubNS_Foo, 0, 0);
property_intersectionProp_type_list->types[1] = (zend_type) ZEND_TYPE_INIT_CLASS(property_intersectionProp_class_ZendTestNS_Bar, 0, 0);
zend_type property_intersectionProp_type = ZEND_TYPE_INIT_INTERSECTION(property_intersectionProp_type_list, 0);
zend_declare_typed_property(class_entry, property_intersectionProp_name, &property_intersectionProp_default_value, ZEND_ACC_PUBLIC, NULL, property_intersectionProp_type);
zend_string_release(property_intersectionProp_name);
zval property_unionProp_default_value;
ZVAL_UNDEF(&property_unionProp_default_value);
zend_string *property_unionProp_name = zend_string_init("unionProp", sizeof("unionProp") - 1, 1);
zend_string *property_unionProp_class_ZendTestNS2_ZendSubNS_Foo = zend_string_init("ZendTestNS2\\ZendSubNS\\Foo", sizeof("ZendTestNS2\\ZendSubNS\\Foo") - 1, 1);
zend_string *property_unionProp_class_ZendTestNS_Bar = zend_string_init("ZendTestNS\\Bar", sizeof("ZendTestNS\\Bar") - 1, 1);
zend_type_list *property_unionProp_type_list = malloc(ZEND_TYPE_LIST_SIZE(2));
property_unionProp_type_list->num_types = 2;
property_unionProp_type_list->types[0] = (zend_type) ZEND_TYPE_INIT_CLASS(property_unionProp_class_ZendTestNS2_ZendSubNS_Foo, 0, 0);
property_unionProp_type_list->types[1] = (zend_type) ZEND_TYPE_INIT_CLASS(property_unionProp_class_ZendTestNS_Bar, 0, 0);
zend_type property_unionProp_type = ZEND_TYPE_INIT_UNION(property_unionProp_type_list, 0);
zend_declare_typed_property(class_entry, property_unionProp_name, &property_unionProp_default_value, ZEND_ACC_PUBLIC, NULL, property_unionProp_type);
zend_string_release(property_unionProp_name);
return class_entry;
}
@@ -18,11 +18,19 @@ var_dump($foo);
object(ZendTestNS2\Foo)#%d (%d) {
["foo"]=>
uninitialized(ZendTestNS2\ZendSubNS\Foo)
["intersectionProp"]=>
uninitialized(ZendTestNS2\ZendSubNS\Foo&ZendTestNS\Bar)
["unionProp"]=>
uninitialized(ZendTestNS2\ZendSubNS\Foo|ZendTestNS\Bar)
}
object(ZendTestNS2\Foo)#%d (%d) {
["foo"]=>
object(ZendTestNS2\ZendSubNS\Foo)#%d (%d) {
}
["intersectionProp"]=>
uninitialized(ZendTestNS2\ZendSubNS\Foo&ZendTestNS\Bar)
["unionProp"]=>
uninitialized(ZendTestNS2\ZendSubNS\Foo|ZendTestNS\Bar)
}
object(ZendTestNS\UnlikelyCompileError)#%d (%d) {
}