Skip to content

Enhance zend_dump_op_array to Properly Represent Non-Printable Characters in String Literals #15680

Closed
@WangYihang

Description

@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:

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

// irrelevant code omited

fwrite($buffer, "--\n");

Current Output:

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:

php_addcslashes(Z_STR_P(zv), "\\\"\n\t\0", 5);

With this change, the output might look like:

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.

References

Footnotes

  1. https://github.com/php/php-src/blob/82c504fa9c36bed95dc28e79b2374889d8a952e7/Zend/Optimizer/zend_dump.c#L70

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions