Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/PhpParser/Node/Scalar/String_.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function parseEscapeSequences(string $str, ?string $quote, bool $p

$extra = '';
if ($parseUnicodeEscape) {
$extra = '|u\{([0-9a-fA-F]+)\}';
$extra = '|u\{([0-9a-fA-F]+)\}|u\{';
}

return preg_replace_callback(
Expand All @@ -131,6 +131,9 @@ function ($matches) {
return chr(hexdec(substr($str, 1)));
}
if ('u' === $str[0]) {
if (!isset($matches[2])) {
throw new Error('Invalid UTF-8 codepoint escape sequence');
}
// PHP 8.6 warns when hexdec() produces a float.
$dec = @hexdec($matches[2]);
// If it overflowed to float, treat as INT_MAX, it will throw an error anyway.
Expand Down
32 changes: 32 additions & 0 deletions test/code/parser/scalar/unicodeEscape.test
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,35 @@ Invalid UTF-8 codepoint escape sequence: Codepoint too large on line 2
"\u{110000}";
-----
Invalid UTF-8 codepoint escape sequence: Codepoint too large on line 2
-----
<?php
"\u{zz}";
-----
Invalid UTF-8 codepoint escape sequence on line 2
-----
<?php
"\u{}";
-----
Invalid UTF-8 codepoint escape sequence on line 2
-----
<?php
"\u{4z}";
-----
Invalid UTF-8 codepoint escape sequence on line 2
-----
<?php
"\u{41";
-----
Invalid UTF-8 codepoint escape sequence on line 2

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth testing "\u{" as well, as the minimal invalid sequence?

On the other hand, I'd drop the tests with variables below -- at least it's not obvious to me what extra coverage they are adding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Added a test for \u{ and removed the cases with variables as suggested.

-----
<?php
"\u{";
-----
Invalid UTF-8 codepoint escape sequence on line 2
-----
<?php
<<<STR
\u{}
STR;
-----
Invalid UTF-8 codepoint escape sequence on line 4
Loading