Skip to content

Commit 4b4ba03

Browse files
authored
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.
1 parent ce3a240 commit 4b4ba03

File tree

7 files changed

+80
-8
lines changed

7 files changed

+80
-8
lines changed

build/gen_stub.php

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

23772377
foreach ($arginfoType->classTypes as $k => $classType) {
2378-
$escapedClassName = $classType->toEscapedName();
2379-
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$escapedClassName}, 0, 0);\n";
2378+
$varEscapedClassName = $classType->toVarEscapedName();
2379+
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->types[$k] = (zend_type) ZEND_TYPE_INIT_CLASS({$variableLikeType}_{$variableLikeName}_class_{$varEscapedClassName}, 0, 0);\n";
23802380
}
23812381

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

ext/zend_test/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static zend_class_entry *zend_test_forbid_dynamic_call;
6565
static zend_class_entry *zend_test_ns_foo_class;
6666
static zend_class_entry *zend_test_ns_unlikely_compile_error_class;
6767
static zend_class_entry *zend_test_ns_not_unlikely_compile_error_class;
68+
static zend_class_entry *zend_test_ns_bar_class;
6869
static zend_class_entry *zend_test_ns2_foo_class;
6970
static zend_class_entry *zend_test_ns2_ns_foo_class;
7071
static zend_class_entry *zend_test_unit_enum;
@@ -1567,6 +1568,7 @@ PHP_MINIT_FUNCTION(zend_test)
15671568
zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
15681569
zend_test_ns_unlikely_compile_error_class = register_class_ZendTestNS_UnlikelyCompileError();
15691570
zend_test_ns_not_unlikely_compile_error_class = register_class_ZendTestNS_NotUnlikelyCompileError();
1571+
zend_test_ns_bar_class = register_class_ZendTestNS_Bar();
15701572
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
15711573
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();
15721574

ext/zend_test/test.stub.php

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

366+
interface Bar {}
367+
366368
class UnlikelyCompileError {
367369
/* This method signature would create a compile error due to the string
368370
* "ZendTestNS\UnlikelyCompileError" in the generated macro call */
@@ -383,6 +385,8 @@ public function method(): ?NotUnlikelyCompileError {}
383385

384386
class Foo {
385387
public ZendSubNS\Foo $foo;
388+
public ZendSubNS\Foo&\ZendTestNS\Bar $intersectionProp;
389+
public ZendSubNS\Foo|\ZendTestNS\Bar $unionProp;
386390

387391
public function method(): void {}
388392
}

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/test_decl.h

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

ext/zend_test/test_legacy_arginfo.h

Lines changed: 23 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)