Skip to content

No escaping in zend_dump_const output #10576

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
SerafimArts opened this issue Feb 13, 2023 · 3 comments · Fixed by #11337
Closed

No escaping in zend_dump_const output #10576

SerafimArts opened this issue Feb 13, 2023 · 3 comments · Fixed by #11337

Comments

@SerafimArts
Copy link
Contributor

SerafimArts commented Feb 13, 2023

Description

The following code:

<?php

substr_count($stmt, '")')

After some optimization steps, it outputs the following opcodes:

0026 SEND_VAR CV1($current) 1
0027 SEND_VAL string("("") 2
0028 V14 = DO_ICALL

Please pay attention to instruction 0027.

This expression cannot be correctly recognized during any automated analysis of the opcode dump. Even if I take the first and last occurrences of string(" and "), then such instructions can disrupt the work of parser:

0001 INIT_STATIC_METHOD_CALL 0 string("")") string("string("")

I understand that from the point of view of grammar, this is impossible in principle, but similar cases may arise.

The problem is here: https://github.com/php/php-src/blob/PHP-8.2/Zend/Optimizer/zend_dump.c#L69

I suggest adding character \\, " (and perhaps \n) escaping so that the output of opcodes can be parsed automatically.

However, before sending a PR, I would like to ask if there is already a ready-made function that escapes the specified sequences, instead of Z_STRVAL_P.

@iluuu1994
Copy link
Member

I'm not sure if this output is really made to be machine readable. Either way, adjusting seems sensible. I don't think there's an existing function that does this.

@SerafimArts
Copy link
Contributor Author

SerafimArts commented Mar 19, 2023

@iluuu1994 hmmm... what about php_addslashes:

php-src/ext/standard/string.c

Lines 3492 to 3494 in 2ec0134

PHPAPI zend_string *php_addslashes(zend_string *str) {
return php_addslashes_ptr(str);
}
?

It is used internally by var_export

ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2);
, so it seems that the string output should be filtered correctly.

@iluuu1994
Copy link
Member

@SerafimArts You could use that, although it requires an allocation per string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants