Bug: Format-preserving printer adds extra backslash when reprinting a modified string node with `\\\` inside in `pSingleQuotedString` · Issue #1144 · nikic/PHP-Parser · GitHub
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Format-preserving printer adds extra backslash when reprinting a modified string node with \\\ inside in pSingleQuotedString #1144
When printFormatPreserving reprints a Scalar\String_ node whose value has changed, it falls back to pSingleQuotedString. That method's regex contains a third alternative (?<=\\)\\ that escapes a backslash simply because it follows another backslash, even when no escaping is required for correctness.
This turns valid non-canonical source like '\\\|' (3 backslashes) into '\\\\|' (4 backslashes), producing a different but semantically equivalent string.
The reprinted string preserves the original three-backslash encoding '\\\|' when format-preserving pretty printer is used.
Actual behaviour
The reprinted string uses four backslashes '\\\\|', adding one backslash that was not
present in the original source.
Root cause
Standard::pSingleQuotedString uses this regex:
$regex = '/\'|\\\\(?=[\'\\\\]|$)|(?<=\\\\)\\\\/';
// ^^^^^^^^^^^^^^ third alternative
Possible fix
For format-preserving pretty printing, we can probably remove the part where extra \ is added:
// before $regex = '/\'|\\\\(?=[\'\\\\]|$)|(?<=\\\\)\\\\/';
// after $regex = '/\'|\\\\(?=[\'\\\\]|$)/';
How to reproduce? I will add the full script that demonstrates a bug, but the crucial thing is that we modify the string (regex) before pretty-printing
Activity