Skip to content

Commit 3c01646

Browse files
alcaeuscmb69
authored andcommitted
Escape \U and \u in generated stubs
This fixes an issue where a namespaced class beginning with "U" or "u" would yield an invalid arginfo file due to the occurrence of a unicode escape sequence, causing a compile error. Co-authored-by: Guilliam Xavier <[email protected]> Closes GH-9154.
1 parent 966d22b commit 3c01646

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

build/gen_stub.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,12 @@ public function toOptimizerTypeMask(): string {
533533
}
534534

535535
public function toEscapedName(): string {
536-
return str_replace('\\', '\\\\', $this->name);
536+
// Escape backslashes, and also encode \u and \U to avoid compilation errors in generated macros
537+
return str_replace(
538+
['\\', '\\u', '\\U'],
539+
['\\\\', '\\\\165', '\\\\125'],
540+
$this->name
541+
);
537542
}
538543

539544
public function toVarEscapedName(): string {

ext/zend_test/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
4343
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
4444
static zend_class_entry *zend_test_forbid_dynamic_call;
4545
static zend_class_entry *zend_test_ns_foo_class;
46+
static zend_class_entry *zend_test_ns_unlikely_compile_error_class;
4647
static zend_class_entry *zend_test_ns2_foo_class;
4748
static zend_class_entry *zend_test_ns2_ns_foo_class;
4849
static zend_class_entry *zend_test_unit_enum;
@@ -535,6 +536,13 @@ static ZEND_METHOD(ZendTestNS_Foo, method)
535536
RETURN_LONG(0);
536537
}
537538

539+
static ZEND_METHOD(ZendTestNS_UnlikelyCompileError, method)
540+
{
541+
ZEND_PARSE_PARAMETERS_NONE();
542+
543+
RETURN_NULL();
544+
}
545+
538546
static ZEND_METHOD(ZendTestNS2_Foo, method)
539547
{
540548
ZEND_PARSE_PARAMETERS_NONE();
@@ -698,6 +706,7 @@ PHP_MINIT_FUNCTION(zend_test)
698706
zend_test_forbid_dynamic_call = register_class_ZendTestForbidDynamicCall();
699707

700708
zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
709+
zend_test_ns_unlikely_compile_error_class = register_class_ZendTestNS_UnlikelyCompileError();
701710
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
702711
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();
703712

ext/zend_test/test.stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ class Foo {
150150
public function method(): int {}
151151
}
152152

153+
class UnlikelyCompileError {
154+
/* This method signature would create a compile error due to the string
155+
* "ZendTestNS\UnlikelyCompileError" in the generated macro call */
156+
public function method(): ?UnlikelyCompileError {}
157+
}
158+
153159
}
154160

155161
namespace ZendTestNS2 {

ext/zend_test/test_arginfo.h

Lines changed: 21 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ $foo = new \ZendTestNS2\Foo();
99
var_dump($foo);
1010
$foo->foo = new \ZendTestNS2\ZendSubNS\Foo();
1111
var_dump($foo);
12+
$foo = new \ZendTestNS\UnlikelyCompileError();
13+
var_dump($foo);
1214
?>
1315
--EXPECTF--
1416
object(ZendTestNS2\Foo)#%d (%d) {
@@ -20,3 +22,5 @@ object(ZendTestNS2\Foo)#%d (%d) {
2022
object(ZendTestNS2\ZendSubNS\Foo)#%d (%d) {
2123
}
2224
}
25+
object(ZendTestNS\UnlikelyCompileError)#%d (%d) {
26+
}

0 commit comments

Comments
 (0)