From bc1c75a15ac2cc0b81e37ebfd4187535cfbdf5e9 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 29 Mar 2017 13:35:53 +0100 Subject: [PATCH] Throw exceptions when failing to deserialize item NBT data, fixed weird crashes when an invalid NBT tag is set on an item --- src/pocketmine/item/Item.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index 356466d687..98e4a82071 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -46,12 +46,22 @@ class Item implements ItemIds, \JsonSerializable{ private static $cachedParser = null; private static function parseCompoundTag(string $tag) : CompoundTag{ + if(strlen($tag) === 0){ + throw new \InvalidArgumentException("No NBT data found in supplied string"); + } + if(self::$cachedParser === null){ self::$cachedParser = new NBT(NBT::LITTLE_ENDIAN); } self::$cachedParser->read($tag); - return self::$cachedParser->getData(); + $data = self::$cachedParser->getData(); + + if(!($data instanceof CompoundTag)){ + throw new \InvalidArgumentException("Invalid item NBT string given, it could not be deserialized"); + } + + return $data; } private static function writeCompoundTag(CompoundTag $tag) : string{