EntityDataHelper: prevent INF/NaN being loaded from disk to come back and break things after the fact

This commit is contained in:
Dylan K. Taylor 2022-03-09 22:16:07 +00:00
parent 8efa299c65
commit 6d584cf008
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -32,6 +32,8 @@ use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\world\World;
use function count;
use function is_infinite;
use function is_nan;
final class EntityDataHelper{
@ -39,6 +41,18 @@ final class EntityDataHelper{
//NOOP
}
/**
* @throws SavedDataLoadingException
*/
private static function validateFloat(string $tagName, string $component, float $value) : void{
if(is_infinite($value)){
throw new SavedDataLoadingException("$component component of '$tagName' contains invalid infinite value");
}
if(is_nan($value)){
throw new SavedDataLoadingException("$component component of '$tagName' contains invalid NaN value");
}
}
/**
* @throws SavedDataLoadingException
*/
@ -54,6 +68,8 @@ final class EntityDataHelper{
if(count($values) !== 2){
throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'");
}
self::validateFloat("Rotation", "yaw", $values[0]->getValue());
self::validateFloat("Rotation", "pitch", $values[1]->getValue());
return Location::fromObject($pos, $world, $values[0]->getValue(), $values[1]->getValue());
}
@ -74,6 +90,15 @@ final class EntityDataHelper{
if(count($values) !== 3){
throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag");
}
return new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue());
$x = $values[0]->getValue();
$y = $values[1]->getValue();
$z = $values[2]->getValue();
self::validateFloat($tagName, "x", $x);
self::validateFloat($tagName, "y", $y);
self::validateFloat($tagName, "z", $z);
return new Vector3($x, $y, $z);
}
}