From 619a9892e55a932a0430fa3fcda5cf8c341b124b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 16 Apr 2020 01:29:28 +0100 Subject: [PATCH] RCON: properly handle potential errors during socket setup --- src/pocketmine/level/format/io/region/RegionLoader.php | 5 ++++- src/pocketmine/network/rcon/RCON.php | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pocketmine/level/format/io/region/RegionLoader.php b/src/pocketmine/level/format/io/region/RegionLoader.php index 87383a3a6..e86d3c56f 100644 --- a/src/pocketmine/level/format/io/region/RegionLoader.php +++ b/src/pocketmine/level/format/io/region/RegionLoader.php @@ -25,6 +25,7 @@ namespace pocketmine\level\format\io\region; use pocketmine\level\format\ChunkException; use pocketmine\level\format\io\exception\CorruptedChunkException; +use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\Binary; use pocketmine\utils\MainLogger; use function ceil; @@ -98,7 +99,9 @@ class RegionLoader{ throw new CorruptedRegionException("Region file should be padded to a multiple of 4KiB"); } - $this->filePointer = fopen($this->filePath, "r+b"); + $filePointer = fopen($this->filePath, "r+b"); + if($filePointer === false) throw new AssumptionFailedError("fopen() should not fail here"); + $this->filePointer = $filePointer; stream_set_read_buffer($this->filePointer, 1024 * 16); //16KB stream_set_write_buffer($this->filePointer, 1024 * 16); //16KB if(!$exists){ diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index 61a123e37..e0522de45 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -75,13 +75,17 @@ class RCON{ throw new \InvalidArgumentException("Empty password"); } - $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + if($socket === false){ + throw new \RuntimeException("Failed to create socket:" . trim(socket_strerror(socket_last_error()))); + } + $this->socket = $socket; if(!socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1)){ throw new \RuntimeException("Unable to set option on socket: " . trim(socket_strerror(socket_last_error()))); } - if($this->socket === false or !@socket_bind($this->socket, $interface, $port) or !@socket_listen($this->socket, 5)){ + if(!@socket_bind($this->socket, $interface, $port) or !@socket_listen($this->socket, 5)){ throw new \RuntimeException(trim(socket_strerror(socket_last_error()))); }