world IO: fixed crashes when garbage data found in tile/entity NBT data

This commit is contained in:
Dylan K. Taylor
2019-12-12 18:21:23 +00:00
parent 9a4b72add5
commit 37a8d95464
3 changed files with 46 additions and 17 deletions

View File

@ -26,6 +26,7 @@ namespace pocketmine\level\format\io\leveldb;
use pocketmine\level\format\Chunk;
use pocketmine\level\format\io\BaseLevelProvider;
use pocketmine\level\format\io\ChunkUtils;
use pocketmine\level\format\io\exception\CorruptedChunkException;
use pocketmine\level\format\io\exception\UnsupportedChunkFormatException;
use pocketmine\level\format\SubChunk;
use pocketmine\level\generator\Flat;
@ -414,24 +415,27 @@ class LevelDB extends BaseLevelProvider{
/** @var CompoundTag[] $entities */
$entities = [];
if(($entityData = $this->db->get($index . self::TAG_ENTITY)) !== false and $entityData !== ""){
$entities = $nbt->read($entityData, true);
if(!is_array($entities)){
$entities = [$entities];
}
}
/** @var CompoundTag $entityNBT */
foreach($entities as $entityNBT){
if($entityNBT->hasTag("id", IntTag::class)){
$entityNBT->setInt("id", $entityNBT->getInt("id") & 0xff); //remove type flags - TODO: use these instead of removing them)
$entityTags = $nbt->read($entityData, true);
foreach((is_array($entityTags) ? $entityTags : [$entityTags]) as $entityTag){
if(!($entityTag instanceof CompoundTag)){
throw new CorruptedChunkException("Entity root tag should be TAG_Compound");
}
if($entityTag->hasTag("id", IntTag::class)){
$entityTag->setInt("id", $entityTag->getInt("id") & 0xff); //remove type flags - TODO: use these instead of removing them)
}
$entities[] = $entityTag;
}
}
/** @var CompoundTag[] $tiles */
$tiles = [];
if(($tileData = $this->db->get($index . self::TAG_BLOCK_ENTITY)) !== false and $tileData !== ""){
$tiles = $nbt->read($tileData, true);
if(!is_array($tiles)){
$tiles = [$tiles];
$tileTags = $nbt->read($tileData, true);
foreach((is_array($tileTags) ? $tileTags : [$tileTags]) as $tileTag){
if(!($tileTag instanceof CompoundTag)){
throw new CorruptedChunkException("Tile root tag should be TAG_Compound");
}
$tiles[] = $tileTag;
}
}