Skip to content

Commit

Permalink
Make sure match is one character long
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Apr 25, 2021
1 parent 33d7c8d commit ce91d13
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,13 +999,13 @@ protected function escapeString($string, $quote) {
}

// Escape control characters and non-UTF-8 characters.
// Regex taken from https://stackoverflow.com/a/11709412/385378.
// Regex based on https://stackoverflow.com/a/11709412/385378.
$regex = '/(
[\x00-\x08\x0E-\x1F] # Control characters
| [\xC0-\xC1] # Invalid UTF-8 Bytes
| [\xF5-\xFF] # Invalid UTF-8 Bytes
| \xE0[\x80-\x9F] # Overlong encoding of prior code point
| \xF0[\x80-\x8F] # Overlong encoding of prior code point
| \xE0(?=[\x80-\x9F]) # Overlong encoding of prior code point
| \xF0(?=[\x80-\x8F]) # Overlong encoding of prior code point
| [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
| [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
| [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
Expand All @@ -1016,6 +1016,7 @@ protected function escapeString($string, $quote) {
| (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
)/x';
return preg_replace_callback($regex, function ($matches) {
assert(strlen($matches[0]) === 1);
$hex = dechex(ord($matches[0]));;
return '\\x' . str_pad($hex, 2, '0', \STR_PAD_LEFT);
}, $escaped);
Expand Down
2 changes: 2 additions & 0 deletions test/code/prettyPrinter/expr/stringEscaping.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Escape sequences in double-quoted strings
"äöü";
"\xc0\x80";
"\xd0\x01";
"\xf0\x80\x80";

<<<DOC
\n\r\t\f\v\$\"\\
Expand All @@ -22,6 +23,7 @@ DOC;
"äöü";
"\xc0\x80";
"\xd0\x01";
"\xf0\x80\x80";
<<<DOC
@@{ "\n\r" }@@\t\f\v\$\\"\\
\x00\x01\x02\x03\x04\x05\x06\x07\x08\t@@{ "\n" }@@\v\f@@{ "\r" }@@\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
Expand Down

0 comments on commit ce91d13

Please sign in to comment.