Make use of Utils::assumeNotFalse() in a bunch of places

I've stuck to only doing this in the places where I'm sure we should never get false back. Other places I'm less sure of (and I found more bugs along the way).
This commit is contained in:
Dylan K. Taylor
2021-12-08 19:33:45 +00:00
parent 8b73549355
commit 889d048ca3
26 changed files with 67 additions and 171 deletions

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\console;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils;
use function fclose;
use function fgets;
use function fopen;
@ -45,9 +45,7 @@ final class ConsoleReader{
fclose($this->stdin);
}
$stdin = fopen("php://stdin", "r");
if($stdin === false) throw new AssumptionFailedError("Opening stdin should never fail");
$this->stdin = $stdin;
$this->stdin = Utils::assumeNotFalse(fopen("php://stdin", "r"), "Opening stdin should never fail");
}
/**

View File

@ -26,6 +26,7 @@ namespace pocketmine\console;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\Thread;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils;
use Webmozart\PathUtil\Path;
use function base64_encode;
use function fgets;
@ -81,21 +82,18 @@ final class ConsoleReaderThread extends Thread{
if($server === false){
throw new \RuntimeException("Failed to open console reader socket server");
}
$address = stream_socket_get_name($server, false);
if($address === false) throw new AssumptionFailedError("stream_socket_get_name() shouldn't return false here");
$address = Utils::assumeNotFalse(stream_socket_get_name($server, false), "stream_socket_get_name() shouldn't return false here");
//Windows sucks, and likes to corrupt UTF-8 file paths when they travel to the subprocess, so we base64 encode
//the path to avoid the problem. This is an abysmally shitty hack, but here we are :(
$sub = proc_open(
$sub = Utils::assumeNotFalse(proc_open(
[PHP_BINARY, '-r', sprintf('require base64_decode("%s", true);', base64_encode(Path::join(__DIR__, 'ConsoleReaderChildProcess.php'))), $address],
[
2 => fopen("php://stderr", "w"),
],
$pipes
);
if($sub === false){
throw new AssumptionFailedError("Something has gone horribly wrong");
}
), "Something has gone horribly wrong");
$client = stream_socket_accept($server, 15);
if($client === false){
throw new AssumptionFailedError("stream_socket_accept() returned false");