Skip to content

Commit

Permalink
Fix json_encode failure for src containing single byte chars with val…
Browse files Browse the repository at this point in the history
…ues greater than 127
  • Loading branch information
johannescoetzee committed Apr 12, 2023
1 parent 0ffddce commit 79424c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion bin/php-parse
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
}
}

spl_autoload_register(function ($class_name) {
$file = __DIR__ . '/../lib/' . $class_name . '.php';
$file = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file);
if (file_exists($file)) {
require $file;
}
});

ini_set('xdebug.max_nesting_level', 3000);

// Disable Xdebug var_dump() output truncation
Expand Down Expand Up @@ -85,7 +93,7 @@ foreach ($files as $file) {
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
} elseif ('json-dump' === $operation) {
fwrite(STDERR, "==> JSON dump:\n");
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
echo json_encode($stmts, JSON_PRETTY_PRINT | JSON_INVALID_UTF8_SUBSTITUTE), "\n";
} elseif ('var-dump' === $operation) {
fwrite(STDERR, "==> var_dump():\n");
var_dump($stmts);
Expand Down
11 changes: 10 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 @@ function($matches) {
if (isset(self::$replacements[$str])) {
return self::$replacements[$str];
} elseif ('x' === $str[0] || 'X' === $str[0]) {
return chr(hexdec(substr($str, 1)));
return self::singleByteHexReplacement(substr($str, 1));
} elseif ('u' === $str[0]) {
return self::codePointToUtf8(hexdec($matches[2]));
} else {
Expand All @@ -127,6 +127,15 @@ function($matches) {
);
}

private static function singleByteHexReplacement(string $hexstr) : string {
$num = hexdec($hexstr);
if ($num <= 0x7F) {
return chr($num);
} else {
return "\x" . $hexstr;
}
}

/**
* Converts a Unicode code point to its UTF-8 encoded representation.
*
Expand Down

0 comments on commit 79424c9

Please sign in to comment.