diff --git a/phpunit.xml b/phpunit.xml index b749148..fa7852f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,6 +4,13 @@ colors="true" stopOnFailure="false" bootstrap="vendor/autoload.php" + failOnDeprecation="true" + failOnEmptyTestSuite="true" + failOnIncomplete="true" + failOnNotice="true" + failOnRisky="true" + failOnSkipped="false" + failOnWarning="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.3/phpunit.xsd"> diff --git a/src/Descriptor/Reader.php b/src/Descriptor/Reader.php index b8292fe..c3809fa 100644 --- a/src/Descriptor/Reader.php +++ b/src/Descriptor/Reader.php @@ -31,9 +31,19 @@ public function read(): ?Descriptor } $offset = 1; + + if (!isset($bytes[$offset])) { + throw new Exception('Failed to read buffer entry ' . $offset); + } + $type = $bytes[$offset]; $offset++; $stdId = Buffer::getString($bytes, 5, $offset); + + if (!isset($bytes[$offset])) { + throw new Exception('Failed to read buffer entry ' . $offset); + } + $version = $bytes[$offset]; $offset++; diff --git a/src/Util/Buffer.php b/src/Util/Buffer.php index c8e5687..eeb684a 100644 --- a/src/Util/Buffer.php +++ b/src/Util/Buffer.php @@ -4,6 +4,8 @@ namespace PhpIso\Util; +use PhpIso\Exception; + class Buffer { /** @@ -28,6 +30,9 @@ public static function getString(array &$buffer, int $length, int &$offset = 0, { $string = ''; for ($i = $offset; $i < $offset + $length; $i++) { + if (!isset($buffer[$i])) { + throw new Exception('Failed to read buffer entry ' . $i); + } $string .= chr($buffer[$i]); } @@ -68,6 +73,9 @@ public static function getBytes(array &$buffer, int $length, int &$offset = 0): { $datas = ''; for ($i = $offset; $i < $offset + $length; $i++) { + if (!isset($buffer[$i])) { + throw new Exception('Failed to read buffer entry ' . $i); + } $datas .= $buffer[$i]; } @@ -89,6 +97,12 @@ public static function readBBO(array &$buffer, int $length, int &$offset = 0): i $len = $length / 2; for ($i = 0; $i < $len; $i++) { + if (!isset($buffer[$offset + ($len - 1 - $i)])) { + throw new Exception('Failed to read buffer entry ' . ($offset + ($len - 1 - $i))); + } + if (!isset($buffer[$offset + $len + $i])) { + throw new Exception('Failed to read buffer entry ' . ($offset + $len + $i)); + } $n1 += $buffer[$offset + ($len - 1 - $i)]; $n2 += $buffer[$offset + $len + $i]; @@ -115,6 +129,10 @@ public static function readLSB(array &$buffer, int $length, int &$offset = 0): i { $lsb = 0; for ($i = 0; $i < $length; $i++) { + if (!isset($buffer[$offset + ($length - 1 - $i)])) { + throw new Exception('Failed to read buffer entry ' . ($offset + ($length - 1 - $i))); + } + $lsb += $buffer[$offset + $length - 1 - $i]; if ($i + 1 < $length) { @@ -135,6 +153,9 @@ public static function readMSB(array &$buffer, int $length, int &$offset = 0): i { $msb = 0; for ($i = 0; $i < $length; $i++) { + if (!isset($buffer[$offset + $i])) { + throw new Exception('Failed to read buffer entry ' . ($offset + $i)); + } $msb += $buffer[$offset + $i]; if ($i + 1 < $length) { @@ -155,6 +176,14 @@ public static function readInt16(array &$buffer, int &$offset = 0): int { $output = 0; + if (!isset($buffer[$offset + 0])) { + throw new Exception('Failed to read buffer entry ' . ($offset + 0)); + } + + if (!isset($buffer[$offset + 1])) { + throw new Exception('Failed to read buffer entry ' . ($offset + 1)); + } + $output += $buffer[$offset + 0] << 8; $output += $buffer[$offset + 1]; @@ -171,6 +200,22 @@ public static function readInt32(array &$buffer, int &$offset = 0): int { $output = 0; + if (!isset($buffer[$offset + 0])) { + throw new Exception('Failed to read buffer entry ' . ($offset + 0)); + } + + if (!isset($buffer[$offset + 1])) { + throw new Exception('Failed to read buffer entry ' . ($offset + 1)); + } + + if (!isset($buffer[$offset + 2])) { + throw new Exception('Failed to read buffer entry ' . ($offset + 2)); + } + + if (!isset($buffer[$offset + 3])) { + throw new Exception('Failed to read buffer entry ' . ($offset + 3)); + } + $output += $buffer[$offset + 0] << 24; $output += $buffer[$offset + 1] << 16; $output += $buffer[$offset + 2] << 8; diff --git a/tests/IsoFileTest.php b/tests/IsoFileTest.php index 34930e3..d090eaa 100644 --- a/tests/IsoFileTest.php +++ b/tests/IsoFileTest.php @@ -18,13 +18,13 @@ public function testConstructorFileDoesNotExist(): void new IsoFile($testFile); } -// public function testConstructorInvalidFile(): void -// { -// $testFile = dirname(__FILE__, 2) . '/fixtures/invalid.iso'; -// -// $this->expectException(Exception::class); -// new IsoFile($testFile); -// } + public function testConstructorInvalidFile(): void + { + $testFile = dirname(__FILE__, 2) . '/fixtures/invalid.iso'; + + $this->expectException(Exception::class); + new IsoFile($testFile); + } public function testConstructorExistingFile(): void {