Skip to content

Commit 9cf4182

Browse files
GromNaNkocsismate
authored andcommitted
gen_stub: fix invalid C variable name for namespaced types in union/intersection type list (php#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.
1 parent cb363ec commit 9cf4182

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

build/gen_stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,8 +2511,8 @@ protected function getTypeCode(string $variableLikeName, string &$code): string
25112511
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->num_types = $classTypeCount;\n";
25122512

25132513
foreach ($arginfoType->classTypes as $k => $classType) {
2514-
$escapedClassName = $classType->toEscapedName();
2515-
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$escapedClassName}, 0, 0);\n";
2514+
$varEscapedClassName = $classType->toVarEscapedName();
2515+
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$varEscapedClassName}, 0, 0);\n";
25162516
}
25172517

25182518
$typeMaskCode = $this->type->toArginfoType()->toTypeMask();

ext/zend_test/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static zend_class_entry *zend_test_forbid_dynamic_call;
6767
static zend_class_entry *zend_test_ns_foo_class;
6868
static zend_class_entry *zend_test_ns_unlikely_compile_error_class;
6969
static zend_class_entry *zend_test_ns_not_unlikely_compile_error_class;
70+
static zend_class_entry *zend_test_ns_bar_class;
7071
static zend_class_entry *zend_test_ns2_foo_class;
7172
static zend_class_entry *zend_test_ns2_ns_foo_class;
7273
static zend_class_entry *zend_test_unit_enum;
@@ -1488,6 +1489,7 @@ PHP_MINIT_FUNCTION(zend_test)
14881489
zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
14891490
zend_test_ns_unlikely_compile_error_class = register_class_ZendTestNS_UnlikelyCompileError();
14901491
zend_test_ns_not_unlikely_compile_error_class = register_class_ZendTestNS_NotUnlikelyCompileError();
1492+
zend_test_ns_bar_class = register_class_ZendTestNS_Bar();
14911493
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
14921494
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();
14931495

ext/zend_test/test.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ class Foo {
361361
public function method(): int {}
362362
}
363363

364+
interface Bar {}
365+
364366
class UnlikelyCompileError {
365367
/* This method signature would create a compile error due to the string
366368
* "ZendTestNS\UnlikelyCompileError" in the generated macro call */
@@ -381,6 +383,8 @@ public function method(): ?NotUnlikelyCompileError {}
381383

382384
class Foo {
383385
public ZendSubNS\Foo $foo;
386+
public ZendSubNS\Foo&\ZendTestNS\Bar $intersectionProp;
387+
public ZendSubNS\Foo|\ZendTestNS\Bar $unionProp;
384388

385389
public function method(): void {}
386390
}

ext/zend_test/test_arginfo.h

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/tests/gen_stub_test_01.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ var_dump($foo);
1818
object(ZendTestNS2\Foo)#%d (%d) {
1919
["foo"]=>
2020
uninitialized(ZendTestNS2\ZendSubNS\Foo)
21+
["intersectionProp"]=>
22+
uninitialized(ZendTestNS2\ZendSubNS\Foo&ZendTestNS\Bar)
23+
["unionProp"]=>
24+
uninitialized(ZendTestNS2\ZendSubNS\Foo|ZendTestNS\Bar)
2125
}
2226
object(ZendTestNS2\Foo)#%d (%d) {
2327
["foo"]=>
2428
object(ZendTestNS2\ZendSubNS\Foo)#%d (%d) {
2529
}
30+
["intersectionProp"]=>
31+
uninitialized(ZendTestNS2\ZendSubNS\Foo&ZendTestNS\Bar)
32+
["unionProp"]=>
33+
uninitialized(ZendTestNS2\ZendSubNS\Foo|ZendTestNS\Bar)
2634
}
2735
object(ZendTestNS\UnlikelyCompileError)#%d (%d) {
2836
}

0 commit comments

Comments
 (0)