Skip to content

Escape \U and \u in generated stubs #9154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,12 @@ public function toOptimizerTypeMask(): string {
}

public function toEscapedName(): string {
return str_replace('\\', '\\\\', $this->name);
// Escape backslashes, and also encode \u and \U to avoid compilation errors in generated macros
return str_replace(
['\\', '\\u', '\\U'],
['\\\\', '\\\\165', '\\\\125'],
$this->name
);
}

public function toVarEscapedName(): string {
Expand Down
9 changes: 9 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
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_ns2_foo_class;
static zend_class_entry *zend_test_ns2_ns_foo_class;
static zend_class_entry *zend_test_unit_enum;
Expand Down Expand Up @@ -535,6 +536,13 @@ static ZEND_METHOD(ZendTestNS_Foo, method)
RETURN_LONG(0);
}

static ZEND_METHOD(ZendTestNS_UnlikelyCompileError, method)
{
ZEND_PARSE_PARAMETERS_NONE();

RETURN_NULL();
}

static ZEND_METHOD(ZendTestNS2_Foo, method)
{
ZEND_PARSE_PARAMETERS_NONE();
Expand Down Expand Up @@ -698,6 +706,7 @@ PHP_MINIT_FUNCTION(zend_test)
zend_test_forbid_dynamic_call = register_class_ZendTestForbidDynamicCall();

zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
zend_test_ns_unlikely_compile_error_class = register_class_ZendTestNS_UnlikelyCompileError();
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();

Expand Down
6 changes: 6 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class Foo {
public function method(): int {}
}

class UnlikelyCompileError {
/* This method signature would create a compile error due to the string
* "ZendTestNS\UnlikelyCompileError" in the generated macro call */
public function method(): ?UnlikelyCompileError {}
}

}

namespace ZendTestNS2 {
Expand Down
22 changes: 21 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ext/zend_test/tests/gen_stub_test_01.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ $foo = new \ZendTestNS2\Foo();
var_dump($foo);
$foo->foo = new \ZendTestNS2\ZendSubNS\Foo();
var_dump($foo);
$foo = new \ZendTestNS\UnlikelyCompileError();
var_dump($foo);
?>
--EXPECTF--
object(ZendTestNS2\Foo)#%d (%d) {
Expand All @@ -20,3 +22,5 @@ object(ZendTestNS2\Foo)#%d (%d) {
object(ZendTestNS2\ZendSubNS\Foo)#%d (%d) {
}
}
object(ZendTestNS\UnlikelyCompileError)#%d (%d) {
}