Skip to content

Fix GH-17831: zend_test_compile_string crash on nul bytes check. #17832

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

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,16 @@ static ZEND_FUNCTION(zend_test_compile_string)

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(source_string)
Z_PARAM_PATH_STR_EX(filename, 1, 0)
Z_PARAM_PATH_STR(filename)
Z_PARAM_LONG(position)
ZEND_PARSE_PARAMETERS_END();

zend_op_array *op_array = NULL;

if (zend_str_has_nul_byte(filename)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using PATH_STR makes this condition not necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it seems PATH_STR does not do the check_null part. no ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_null means checking for a NULL argument instead of a string, not for NUL bytes.
The bug here was that you passed 1 to check_null instead of 0. And passing 0 is equivalent to PATH_STR without the EX

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah gotcha yes not the same thing at 2nd look.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole reason we have PATH_STR is to check for NUL bytes in the string, that's the only difference with normal STR fast-ZPP

return;
}

op_array = compile_string(source_string, ZSTR_VAL(filename), position);

if (op_array) {
Expand Down
15 changes: 14 additions & 1 deletion ext/zend_test/tests/zend_test_compile_string.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ try {
echo $e->getMessage(), PHP_EOL;
}

zend_test_compile_string($x, $y, $z);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just pass NULLs to avoid the warning on undef var


$source_string = <<<EOF
<?php
Expand All @@ -58,12 +59,24 @@ EOF;
zend_test_compile_string($source_string, 'Source string', ZEND_COMPILE_POSITION_AFTER_OPEN_TAG);

?>
--EXPECT--
--EXPECTF--
string(3) "php"
#!/path/to/php
string(3) "php"
string(3) "php"
string(3) "php"
zend_test_compile_string(): Argument #2 ($filename) must not contain any null bytes

Warning: Undefined variable $x in %s on line %d

Warning: Undefined variable $y in %s on line %d

Warning: Undefined variable $z in %s on line %d

Deprecated: zend_test_compile_string(): Passing null to parameter #1 ($source_string) of type string is deprecated in %s on line %d

Deprecated: zend_test_compile_string(): Passing null to parameter #2 ($filename) of type string is deprecated in %s on line %d

Deprecated: zend_test_compile_string(): Passing null to parameter #3 ($position) of type int is deprecated in %s on line %d

Parse error: syntax error, unexpected token "<", expecting end of file in Source string on line 1
Loading