gen_stub: Fix handling of escape sequences in generated C strings#22273
Open
NattyNarwhal wants to merge 1 commit into
Open
gen_stub: Fix handling of escape sequences in generated C strings#22273NattyNarwhal wants to merge 1 commit into
NattyNarwhal wants to merge 1 commit into
Conversation
5cb8e03 to
b1e2fe2
Compare
Member
Add it to the ext/zend_test stub. |
mvorisek
reviewed
Jun 11, 2026
kocsismate
reviewed
Jun 13, 2026
kocsismate
left a comment
Member
There was a problem hiding this comment.
but LGTM otherwise, thank you!
8ac2225 to
3e50b9c
Compare
When handling sequences like this in a stub:
```php
<?php
class Whatever {
public static string $foobar1 = "CCC \n\r\t\v\e\f\\\$\"\101\x41\u{41} CCC";
public static string $foobar2 = 'CCC \n\r\t\v\e\f\\\$\"\101\x41\u{41} CCC';
}
```
...properly generate C headers that properly escape the string.
Otherwise, the differing escaping rules and differences between PHP's
single and double quoted strings could lead to mangled headers.
The output of these strings after the stub has been generated:
```
string(22) "BEGIN
��\$"AAA END"
string(43) "BEGIN \n\r\t\v\e\f\\\\$\"\101\x41\u{41} END"
```
And the generated arginfo:
```c
zval property_doubleQuoteEscaped_default_value;
zend_string *property_doubleQuoteEscaped_default_value_str = zend_string_init("BEGIN \n\r\t\v\x1b\f\\$\"AAA END", strlen("BEGIN \n\r\t\v\x1b\f\\$\"AAA END"), 1);
pp ZVAL_STR(&property_doubleQuoteEscaped_default_value, property_doubleQuoteEscaped_default_value_str);
zend_string *property_doubleQuoteEscaped_name = zend_string_init("doubleQuoteEscaped", sizeof("doubleQuoteEscaped") - 1, true);
zend_declare_typed_property(class_entry, property_doubleQuoteEscaped_name, &property_doubleQuoteEscaped_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release_ex(property_doubleQuoteEscaped_name, true);
zval property_singleQuoteEscaped_default_value;
zend_string *property_singleQuoteEscaped_default_value_str = zend_string_init("BEGIN \\n\\r\\t\\v\\e\\f\\\\\\\\$\\\"\\101\\x41\\u{41} END", strlen("BEGIN \\n\\r\\t\\v\\e\\f\\\\\\\\$\\\"\\101\\x41\\u{41} END"), 1);
ZVAL_STR(&property_singleQuoteEscaped_default_value, property_singleQuoteEscaped_default_value_str);
zend_string *property_singleQuoteEscaped_name = zend_string_init("singleQuoteEscaped", sizeof("singleQuoteEscaped") - 1, true);
zend_declare_typed_property(class_entry, property_singleQuoteEscaped_name, &property_singleQuoteEscaped_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release_ex(property_singleQuoteEscaped_name, true);
```
Note that the PHP escape sequence "\$" will be handled in a special
manner to avoid providing it to C, to avoid C compiler warnings.
Tests are included via the zend_test stub and a phpt file to ensure that
the escape sequences match.
Fixes phpGH-22169.
3e50b9c to
debc17c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When handling sequences like this in a stub:
...properly generate C headers that properly escape the string. Otherwise, the differing escaping rules and differences between PHP's single and double quoted strings could lead to mangled headers.
The output of these strings after the stub has been generated:
And the generated arginfo:
Note that the PHP escape sequence "$" will be handled in a special manner to avoid providing it to C, to avoid C compiler warnings.
Tests are included via the zend_test stub and a phpt file to ensure that the escape sequences match.
Fixes GH-22169.