Harden APIs which accept Vector3/Position/Location in event namespace

This commit is contained in:
Dylan K. Taylor 2022-03-09 22:22:37 +00:00
parent 879476d8e0
commit f97ce6afef
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 35 additions and 0 deletions

View File

@ -27,6 +27,7 @@ use pocketmine\block\Block;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\math\Vector3;
use pocketmine\utils\Utils;
class BlockTeleportEvent extends BlockEvent implements Cancellable{
use CancellableTrait;
@ -44,6 +45,7 @@ class BlockTeleportEvent extends BlockEvent implements Cancellable{
}
public function setTo(Vector3 $to) : void{
Utils::checkVector3NotInfOrNaN($to);
$this->to = $to;
}
}

View File

@ -26,6 +26,7 @@ namespace pocketmine\event\entity;
use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\utils\Utils;
use pocketmine\world\Position;
/**
@ -54,6 +55,7 @@ class EntityTeleportEvent extends EntityEvent implements Cancellable{
}
public function setTo(Position $to) : void{
Utils::checkVector3NotInfOrNaN($to);
$this->to = $to;
}
}

View File

@ -27,6 +27,7 @@ use pocketmine\entity\Location;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\player\Player;
use pocketmine\utils\Utils;
class PlayerMoveEvent extends PlayerEvent implements Cancellable{
use CancellableTrait;
@ -51,6 +52,7 @@ class PlayerMoveEvent extends PlayerEvent implements Cancellable{
}
public function setTo(Location $to) : void{
Utils::checkLocationNotInfOrNaN($to);
$this->to = $to;
}
}

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\event\player;
use pocketmine\player\Player;
use pocketmine\utils\Utils;
use pocketmine\world\Position;
/**
@ -46,6 +47,7 @@ class PlayerRespawnEvent extends PlayerEvent{
if(!$position->isValid()){
throw new \InvalidArgumentException("Spawn position must reference a valid and loaded World");
}
Utils::checkVector3NotInfOrNaN($position);
$this->position = $position;
}
}

View File

@ -28,7 +28,9 @@ declare(strict_types=1);
namespace pocketmine\utils;
use DaveRandom\CallbackValidator\CallbackType;
use pocketmine\entity\Location;
use pocketmine\errorhandler\ErrorTypeToStringMap;
use pocketmine\math\Vector3;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function array_combine;
@ -57,7 +59,9 @@ use function interface_exists;
use function is_a;
use function is_array;
use function is_bool;
use function is_infinite;
use function is_int;
use function is_nan;
use function is_object;
use function is_string;
use function mb_check_encoding;
@ -602,4 +606,27 @@ final class Utils{
}
return $value;
}
public static function checkFloatNotInfOrNaN(string $name, float $float) : void{
if(is_nan($float)){
throw new \InvalidArgumentException("$name cannot be NaN");
}
if(is_infinite($float)){
throw new \InvalidArgumentException("$name cannot be infinite");
}
}
public static function checkVector3NotInfOrNaN(Vector3 $vector3) : void{
if($vector3 instanceof Location){ //location could be masquerading as vector3
self::checkFloatNotInfOrNaN("yaw", $vector3->yaw);
self::checkFloatNotInfOrNaN("pitch", $vector3->pitch);
}
self::checkFloatNotInfOrNaN("x", $vector3->x);
self::checkFloatNotInfOrNaN("y", $vector3->y);
self::checkFloatNotInfOrNaN("z", $vector3->z);
}
public static function checkLocationNotInfOrNaN(Location $location) : void{
self::checkVector3NotInfOrNaN($location);
}
}