[php-src] Issue #15680: Enhance `zend_dump_op_array` to Properly Represent Non-Printable Characters in String Literals
| From: | WangYihang | Date: | Sat, 31 Aug 2024 13:17:14 +0000 |
| Subject: | [php-src] Issue #15680: Enhance `zend_dump_op_array` to Properly Represent Non-Printable Characters in String Literals | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247660@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15680
Author: WangYihang
### Description
Description:
While working with the zend_dump_op_array function in PHP's Zend Engine, I noticed that
non-printable characters in string literals are not always represented in their escaped (readable)
form. Currently, the function escapes [^1] only a limited set of characters by calling:
```c
zend_string *escaped_string = php_addcslashes(Z_STR_P(zv), "\"\\", 2);
```
This can result in the direct output of characters such as newlines, tabs, and null bytes, which
might make the dump less clear and harder to debug.
For example, for the following php code:
```php
<?php
// irrelavent code omited
fwrite($buffer, "--\n");
```
Current Output:
```c
0040 INIT_FCALL 2 112 string("fwrite")
0041 SEND_VAR CV6($buffer) 1
0042 SEND_VAL string("--
") 2 <------ new line (\n) is directly printed
0043 DO_ICALL
```
Expected Output:
I believe that modifying the function to escape all non-printable characters would enhance the
clarity of the output. This could be achieved by updating the call to:
```c
php_addcslashes(Z_STR_P(zv), "\\\"\n\t\0", 5);
```
With this change, the output might look like:
```c
0040 INIT_FCALL 2 112 string("fwrite")
0041 SEND_VAR CV6($buffer) 1
0042 SEND_VAL string("--\n") 2 <------ properly escaped with readable
representations
0043 DO_ICALL
```
Proposal:
I propose a small enhancement to the zend_dump_op_array function to ensure that all non-printable
characters are properly represented in the output. This adjustment would improve readability and
make debugging easier, without significantly altering the existing functionality.
Benefits:
Enhanced Readability: Ensures that non-printable characters are clearly represented, making the
output easier to understand.
Improved Debugging: Developers can quickly identify and interpret all characters within string
literals.
Minor Change: The proposed adjustment is a minimal change to the existing codebase but offers
significant benefits.
I would greatly appreciate feedback from the community on this suggestion.
If there is agreement, I would be happy to submit a pull request to implement this improvement.
References
[^1]: https://github.com/php/php-src/blob/82c504fa9c36bed95dc28e79b2374889d8a952e7/Zend/Optimizer/zend_dump.c#L70