Use JSON_THROW_ON_ERROR for json_encode() and json_decode()

This commit is contained in:
Dylan K. Taylor 2021-12-08 19:14:07 +00:00
parent c6466a6da9
commit 8b73549355
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
9 changed files with 20 additions and 51 deletions

View File

@ -40,4 +40,4 @@ echo json_encode([
"details_url" => "https://github.com/$argv[3]/releases/tag/$argv[2]", "details_url" => "https://github.com/$argv[3]/releases/tag/$argv[2]",
"download_url" => "https://github.com/$argv[3]/releases/download/$argv[2]/PocketMine-MP.phar", "download_url" => "https://github.com/$argv[3]/releases/download/$argv[2]/PocketMine-MP.phar",
"source_url" => "https://github.com/$argv[3]/tree/$argv[2]", "source_url" => "https://github.com/$argv[3]/tree/$argv[2]",
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"; ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n";

View File

@ -65,6 +65,7 @@ use function sprintf;
use function strlen; use function strlen;
use function substr; use function substr;
use const JSON_PRETTY_PRINT; use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_SLASHES;
use const SORT_NUMERIC; use const SORT_NUMERIC;
@ -349,7 +350,7 @@ class MemoryManager{
} }
} }
file_put_contents(Path::join($outputFolder, "staticProperties.js"), json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents(Path::join($outputFolder, "staticProperties.js"), json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
$logger->info("Wrote $staticCount static properties"); $logger->info("Wrote $staticCount static properties");
$globalVariables = []; $globalVariables = [];
@ -376,7 +377,7 @@ class MemoryManager{
$globalVariables[$varName] = self::continueDump($value, $objects, $refCounts, 0, $maxNesting, $maxStringSize); $globalVariables[$varName] = self::continueDump($value, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
} }
file_put_contents(Path::join($outputFolder, "globalVariables.js"), json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents(Path::join($outputFolder, "globalVariables.js"), json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
$logger->info("Wrote $globalCount global variables"); $logger->info("Wrote $globalCount global variables");
foreach(get_defined_functions()["user"] as $function){ foreach(get_defined_functions()["user"] as $function){
@ -391,7 +392,7 @@ class MemoryManager{
$functionStaticVarsCount += count($vars); $functionStaticVarsCount += count($vars);
} }
} }
file_put_contents(Path::join($outputFolder, 'functionStaticVars.js'), json_encode($functionStaticVars, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents(Path::join($outputFolder, 'functionStaticVars.js'), json_encode($functionStaticVars, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
$logger->info("Wrote $functionStaticVarsCount function static variables"); $logger->info("Wrote $functionStaticVarsCount function static variables");
$data = self::continueDump($startingObject, $objects, $refCounts, 0, $maxNesting, $maxStringSize); $data = self::continueDump($startingObject, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
@ -454,7 +455,7 @@ class MemoryManager{
} }
} }
fwrite($obData, json_encode($info, JSON_UNESCAPED_SLASHES) . "\n"); fwrite($obData, json_encode($info, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n");
} }
}while($continue); }while($continue);
@ -463,11 +464,11 @@ class MemoryManager{
fclose($obData); fclose($obData);
file_put_contents(Path::join($outputFolder, "serverEntry.js"), json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents(Path::join($outputFolder, "serverEntry.js"), json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
file_put_contents(Path::join($outputFolder, "referenceCounts.js"), json_encode($refCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents(Path::join($outputFolder, "referenceCounts.js"), json_encode($refCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
arsort($instanceCounts, SORT_NUMERIC); arsort($instanceCounts, SORT_NUMERIC);
file_put_contents(Path::join($outputFolder, "instanceCounts.js"), json_encode($instanceCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents(Path::join($outputFolder, "instanceCounts.js"), json_encode($instanceCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
$logger->info("Finished!"); $logger->info("Finished!");

View File

@ -41,7 +41,6 @@ use function file_exists;
use function file_get_contents; use function file_get_contents;
use function get_loaded_extensions; use function get_loaded_extensions;
use function json_encode; use function json_encode;
use function json_last_error_msg;
use function ksort; use function ksort;
use function max; use function max;
use function mb_strtoupper; use function mb_strtoupper;
@ -60,6 +59,7 @@ use function substr;
use function zend_version; use function zend_version;
use function zlib_encode; use function zlib_encode;
use const FILE_IGNORE_NEW_LINES; use const FILE_IGNORE_NEW_LINES;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_SLASHES;
use const PHP_OS; use const PHP_OS;
use const PHP_VERSION; use const PHP_VERSION;
@ -104,10 +104,7 @@ class CrashDump{
$this->extraData(); $this->extraData();
$json = json_encode($this->data, JSON_UNESCAPED_SLASHES); $json = json_encode($this->data, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
if($json === false){
throw new \RuntimeException("Failed to encode crashdump JSON: " . json_last_error_msg());
}
$zlibEncoded = zlib_encode($json, ZLIB_ENCODING_DEFLATE, 9); $zlibEncoded = zlib_encode($json, ZLIB_ENCODING_DEFLATE, 9);
if($zlibEncoded === false) throw new AssumptionFailedError("ZLIB compression failed"); if($zlibEncoded === false) throw new AssumptionFailedError("ZLIB compression failed");
$this->encodedData = $zlibEncoded; $this->encodedData = $zlibEncoded;

View File

@ -29,6 +29,7 @@ use function in_array;
use function json_encode; use function json_encode;
use function json_last_error_msg; use function json_last_error_msg;
use function strlen; use function strlen;
use const JSON_THROW_ON_ERROR;
final class Skin{ final class Skin{
public const ACCEPTED_SKIN_SIZES = [ public const ACCEPTED_SKIN_SIZES = [
@ -73,7 +74,7 @@ final class Skin{
* Not only that, they are pretty-printed. * Not only that, they are pretty-printed.
* TODO: find out what model crap can be safely dropped from the packet (unless it gets fixed first) * TODO: find out what model crap can be safely dropped from the packet (unless it gets fixed first)
*/ */
$geometryData = json_encode($decodedGeometry); $geometryData = json_encode($decodedGeometry, JSON_THROW_ON_ERROR);
} }
$this->skinId = $skinId; $this->skinId = $skinId;

View File

@ -124,12 +124,12 @@ use function count;
use function get_class; use function get_class;
use function in_array; use function in_array;
use function json_encode; use function json_encode;
use function json_last_error_msg;
use function strlen; use function strlen;
use function strtolower; use function strtolower;
use function substr; use function substr;
use function time; use function time;
use function ucfirst; use function ucfirst;
use const JSON_THROW_ON_ERROR;
class NetworkSession{ class NetworkSession{
private \PrefixedLogger $logger; private \PrefixedLogger $logger;
@ -900,11 +900,7 @@ class NetworkSession{
} }
public function onFormSent(int $id, Form $form) : bool{ public function onFormSent(int $id, Form $form) : bool{
$formData = json_encode($form); return $this->sendDataPacket(ModalFormRequestPacket::create($id, json_encode($form, JSON_THROW_ON_ERROR)));
if($formData === false){
throw new \InvalidArgumentException("Failed to encode form JSON: " . json_last_error_msg());
}
return $this->sendDataPacket(ModalFormRequestPacket::create($id, $formData));
} }
/** /**

View File

@ -31,9 +31,9 @@ use function is_array;
use function is_string; use function is_string;
use function json_decode; use function json_decode;
use function json_encode; use function json_encode;
use function json_last_error_msg;
use function random_bytes; use function random_bytes;
use function str_repeat; use function str_repeat;
use const JSON_THROW_ON_ERROR;
class LegacySkinAdapter implements SkinAdapter{ class LegacySkinAdapter implements SkinAdapter{
@ -44,14 +44,10 @@ class LegacySkinAdapter implements SkinAdapter{
if($geometryName === ""){ if($geometryName === ""){
$geometryName = "geometry.humanoid.custom"; $geometryName = "geometry.humanoid.custom";
} }
$resourcePatch = json_encode(["geometry" => ["default" => $geometryName]]);
if($resourcePatch === false){
throw new \RuntimeException("json_encode() failed: " . json_last_error_msg());
}
return new SkinData( return new SkinData(
$skin->getSkinId(), $skin->getSkinId(),
"", //TODO: playfab ID "", //TODO: playfab ID
$resourcePatch, json_encode(["geometry" => ["default" => $geometryName]], JSON_THROW_ON_ERROR),
SkinImage::fromLegacy($skin->getSkinData()), [], SkinImage::fromLegacy($skin->getSkinData()), [],
$capeImage, $capeImage,
$skin->getGeometryData() $skin->getGeometryData()

View File

@ -27,7 +27,6 @@ use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\scheduler\AsyncTask; use pocketmine\scheduler\AsyncTask;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Internet; use pocketmine\utils\Internet;
use pocketmine\utils\Process; use pocketmine\utils\Process;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
@ -37,11 +36,11 @@ use function array_map;
use function array_values; use function array_values;
use function count; use function count;
use function json_encode; use function json_encode;
use function json_last_error_msg;
use function md5; use function md5;
use function microtime; use function microtime;
use function php_uname; use function php_uname;
use function strlen; use function strlen;
use const JSON_THROW_ON_ERROR;
use const PHP_VERSION; use const PHP_VERSION;
class SendUsageTask extends AsyncTask{ class SendUsageTask extends AsyncTask{
@ -150,9 +149,7 @@ class SendUsageTask extends AsyncTask{
} }
$this->endpoint = $endpoint . "api/post"; $this->endpoint = $endpoint . "api/post";
$data = json_encode($data/*, JSON_PRETTY_PRINT*/); $this->data = json_encode($data, /*JSON_PRETTY_PRINT |*/ JSON_THROW_ON_ERROR);
if($data === false) throw new AssumptionFailedError("Statistics JSON should never fail to encode: " . json_last_error_msg());
$this->data = $data;
} }
public function onRun() : void{ public function onRun() : void{

View File

@ -40,31 +40,16 @@ parameters:
count: 1 count: 1
path: ../../../src/PocketMine.php path: ../../../src/PocketMine.php
-
message: "#^Parameter \\#1 \\$haystack of function substr_count expects string, string\\|false given\\.$#"
count: 1
path: ../../../src/PocketMine.php
- -
message: "#^Parameter \\#1 \\$path of function realpath expects string, string\\|false given\\.$#" message: "#^Parameter \\#1 \\$path of function realpath expects string, string\\|false given\\.$#"
count: 2 count: 2
path: ../../../src/PocketMine.php path: ../../../src/PocketMine.php
-
message: "#^Parameter \\#1 \\$version1 of function version_compare expects string, string\\|false given\\.$#"
count: 3
path: ../../../src/PocketMine.php
- -
message: "#^Cannot cast mixed to string\\.$#" message: "#^Cannot cast mixed to string\\.$#"
count: 1 count: 1
path: ../../../src/Server.php path: ../../../src/Server.php
-
message: "#^Only numeric types are allowed in \\+, int\\|false given on the left side\\.$#"
count: 1
path: ../../../src/Server.php
- -
message: "#^Parameter \\#1 \\$array of static method pocketmine\\\\plugin\\\\PluginGraylist\\:\\:fromArray\\(\\) expects array, mixed given\\.$#" message: "#^Parameter \\#1 \\$array of static method pocketmine\\\\plugin\\\\PluginGraylist\\:\\:fromArray\\(\\) expects array, mixed given\\.$#"
count: 1 count: 1
@ -545,11 +530,6 @@ parameters:
count: 1 count: 1
path: ../../../src/entity/Living.php path: ../../../src/entity/Living.php
-
message: "#^Property pocketmine\\\\entity\\\\Skin\\:\\:\\$geometryData \\(string\\) does not accept string\\|false\\.$#"
count: 1
path: ../../../src/entity/Skin.php
- -
message: "#^Parameter \\#2 \\$x of method pocketmine\\\\block\\\\Block\\:\\:position\\(\\) expects int, float\\|int given\\.$#" message: "#^Parameter \\#2 \\$x of method pocketmine\\\\block\\\\Block\\:\\:position\\(\\) expects int, float\\|int given\\.$#"
count: 1 count: 1

View File

@ -72,4 +72,5 @@ file_put_contents(__DIR__ . '/block_factory_consistency_check.json', json_encode
"knownStates" => $new, "knownStates" => $new,
"remaps" => $remaps "remaps" => $remaps
], ],
JSON_THROW_ON_ERROR
)); ));