From 8b7354935574ed8955d033aa21d96cc7ad54a66c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 8 Dec 2021 19:14:07 +0000 Subject: [PATCH] Use JSON_THROW_ON_ERROR for json_encode() and json_decode() --- build/generate-build-info-json.php | 2 +- src/MemoryManager.php | 15 +++++++------- src/crash/CrashDump.php | 7 ++----- src/entity/Skin.php | 3 ++- src/network/mcpe/NetworkSession.php | 8 ++------ .../mcpe/convert/LegacySkinAdapter.php | 8 ++------ src/stats/SendUsageTask.php | 7 ++----- tests/phpstan/configs/actual-problems.neon | 20 ------------------- .../block/regenerate_consistency_check.php | 1 + 9 files changed, 20 insertions(+), 51 deletions(-) diff --git a/build/generate-build-info-json.php b/build/generate-build-info-json.php index 00ca2adc8..dd353fd7c 100644 --- a/build/generate-build-info-json.php +++ b/build/generate-build-info-json.php @@ -40,4 +40,4 @@ echo json_encode([ "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", "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"; diff --git a/src/MemoryManager.php b/src/MemoryManager.php index 020532f82..3bf0f5455 100644 --- a/src/MemoryManager.php +++ b/src/MemoryManager.php @@ -65,6 +65,7 @@ use function sprintf; use function strlen; use function substr; use const JSON_PRETTY_PRINT; +use const JSON_THROW_ON_ERROR; use const JSON_UNESCAPED_SLASHES; 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"); $globalVariables = []; @@ -376,7 +377,7 @@ class MemoryManager{ $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"); foreach(get_defined_functions()["user"] as $function){ @@ -391,7 +392,7 @@ class MemoryManager{ $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"); $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); @@ -463,11 +464,11 @@ class MemoryManager{ 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, "referenceCounts.js"), json_encode($refCounts, 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 | JSON_THROW_ON_ERROR)); 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!"); diff --git a/src/crash/CrashDump.php b/src/crash/CrashDump.php index f5a3213f1..f72c05fec 100644 --- a/src/crash/CrashDump.php +++ b/src/crash/CrashDump.php @@ -41,7 +41,6 @@ use function file_exists; use function file_get_contents; use function get_loaded_extensions; use function json_encode; -use function json_last_error_msg; use function ksort; use function max; use function mb_strtoupper; @@ -60,6 +59,7 @@ use function substr; use function zend_version; use function zlib_encode; use const FILE_IGNORE_NEW_LINES; +use const JSON_THROW_ON_ERROR; use const JSON_UNESCAPED_SLASHES; use const PHP_OS; use const PHP_VERSION; @@ -104,10 +104,7 @@ class CrashDump{ $this->extraData(); - $json = json_encode($this->data, JSON_UNESCAPED_SLASHES); - if($json === false){ - throw new \RuntimeException("Failed to encode crashdump JSON: " . json_last_error_msg()); - } + $json = json_encode($this->data, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); $zlibEncoded = zlib_encode($json, ZLIB_ENCODING_DEFLATE, 9); if($zlibEncoded === false) throw new AssumptionFailedError("ZLIB compression failed"); $this->encodedData = $zlibEncoded; diff --git a/src/entity/Skin.php b/src/entity/Skin.php index 0542bd75a..d9489772f 100644 --- a/src/entity/Skin.php +++ b/src/entity/Skin.php @@ -29,6 +29,7 @@ use function in_array; use function json_encode; use function json_last_error_msg; use function strlen; +use const JSON_THROW_ON_ERROR; final class Skin{ public const ACCEPTED_SKIN_SIZES = [ @@ -73,7 +74,7 @@ final class Skin{ * 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) */ - $geometryData = json_encode($decodedGeometry); + $geometryData = json_encode($decodedGeometry, JSON_THROW_ON_ERROR); } $this->skinId = $skinId; diff --git a/src/network/mcpe/NetworkSession.php b/src/network/mcpe/NetworkSession.php index 0b7a8253b..66b77fbb3 100644 --- a/src/network/mcpe/NetworkSession.php +++ b/src/network/mcpe/NetworkSession.php @@ -124,12 +124,12 @@ use function count; use function get_class; use function in_array; use function json_encode; -use function json_last_error_msg; use function strlen; use function strtolower; use function substr; use function time; use function ucfirst; +use const JSON_THROW_ON_ERROR; class NetworkSession{ private \PrefixedLogger $logger; @@ -900,11 +900,7 @@ class NetworkSession{ } public function onFormSent(int $id, Form $form) : bool{ - $formData = json_encode($form); - if($formData === false){ - throw new \InvalidArgumentException("Failed to encode form JSON: " . json_last_error_msg()); - } - return $this->sendDataPacket(ModalFormRequestPacket::create($id, $formData)); + return $this->sendDataPacket(ModalFormRequestPacket::create($id, json_encode($form, JSON_THROW_ON_ERROR))); } /** diff --git a/src/network/mcpe/convert/LegacySkinAdapter.php b/src/network/mcpe/convert/LegacySkinAdapter.php index 31d4ae92b..60bb29d57 100644 --- a/src/network/mcpe/convert/LegacySkinAdapter.php +++ b/src/network/mcpe/convert/LegacySkinAdapter.php @@ -31,9 +31,9 @@ use function is_array; use function is_string; use function json_decode; use function json_encode; -use function json_last_error_msg; use function random_bytes; use function str_repeat; +use const JSON_THROW_ON_ERROR; class LegacySkinAdapter implements SkinAdapter{ @@ -44,14 +44,10 @@ class LegacySkinAdapter implements SkinAdapter{ if($geometryName === ""){ $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( $skin->getSkinId(), "", //TODO: playfab ID - $resourcePatch, + json_encode(["geometry" => ["default" => $geometryName]], JSON_THROW_ON_ERROR), SkinImage::fromLegacy($skin->getSkinData()), [], $capeImage, $skin->getGeometryData() diff --git a/src/stats/SendUsageTask.php b/src/stats/SendUsageTask.php index 9440ae779..2064caf08 100644 --- a/src/stats/SendUsageTask.php +++ b/src/stats/SendUsageTask.php @@ -27,7 +27,6 @@ use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\player\Player; use pocketmine\scheduler\AsyncTask; use pocketmine\Server; -use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\Internet; use pocketmine\utils\Process; use pocketmine\utils\Utils; @@ -37,11 +36,11 @@ use function array_map; use function array_values; use function count; use function json_encode; -use function json_last_error_msg; use function md5; use function microtime; use function php_uname; use function strlen; +use const JSON_THROW_ON_ERROR; use const PHP_VERSION; class SendUsageTask extends AsyncTask{ @@ -150,9 +149,7 @@ class SendUsageTask extends AsyncTask{ } $this->endpoint = $endpoint . "api/post"; - $data = json_encode($data/*, JSON_PRETTY_PRINT*/); - if($data === false) throw new AssumptionFailedError("Statistics JSON should never fail to encode: " . json_last_error_msg()); - $this->data = $data; + $this->data = json_encode($data, /*JSON_PRETTY_PRINT |*/ JSON_THROW_ON_ERROR); } public function onRun() : void{ diff --git a/tests/phpstan/configs/actual-problems.neon b/tests/phpstan/configs/actual-problems.neon index 52b727319..bbf723ac2 100644 --- a/tests/phpstan/configs/actual-problems.neon +++ b/tests/phpstan/configs/actual-problems.neon @@ -40,31 +40,16 @@ parameters: count: 1 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\\.$#" count: 2 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\\.$#" count: 1 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\\.$#" count: 1 @@ -545,11 +530,6 @@ parameters: count: 1 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\\.$#" count: 1 diff --git a/tests/phpunit/block/regenerate_consistency_check.php b/tests/phpunit/block/regenerate_consistency_check.php index c8dd68dc5..c18b94532 100644 --- a/tests/phpunit/block/regenerate_consistency_check.php +++ b/tests/phpunit/block/regenerate_consistency_check.php @@ -72,4 +72,5 @@ file_put_contents(__DIR__ . '/block_factory_consistency_check.json', json_encode "knownStates" => $new, "remaps" => $remaps ], + JSON_THROW_ON_ERROR ));