Skip to content

Commit 231e8e9

Browse files
committed
Added more validations
1 parent 547606d commit 231e8e9

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
colors="true"
55
stopOnFailure="false"
66
bootstrap="vendor/autoload.php"
7+
failOnDeprecation="true"
8+
failOnEmptyTestSuite="true"
9+
failOnIncomplete="true"
10+
failOnNotice="true"
11+
failOnRisky="true"
12+
failOnSkipped="false"
13+
failOnWarning="true"
714
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.3/phpunit.xsd">
815
<source>
916
<include>

src/Descriptor/Reader.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,19 @@ public function read(): ?Descriptor
3131
}
3232

3333
$offset = 1;
34+
35+
if (!isset($bytes[$offset])) {
36+
throw new Exception('Failed to read buffer entry ' . $offset);
37+
}
38+
3439
$type = $bytes[$offset];
3540
$offset++;
3641
$stdId = Buffer::getString($bytes, 5, $offset);
42+
43+
if (!isset($bytes[$offset])) {
44+
throw new Exception('Failed to read buffer entry ' . $offset);
45+
}
46+
3747
$version = $bytes[$offset];
3848
$offset++;
3949

src/Util/Buffer.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpIso\Util;
66

7+
use PhpIso\Exception;
8+
79
class Buffer
810
{
911
/**
@@ -28,6 +30,9 @@ public static function getString(array &$buffer, int $length, int &$offset = 0,
2830
{
2931
$string = '';
3032
for ($i = $offset; $i < $offset + $length; $i++) {
33+
if (!isset($buffer[$i])) {
34+
throw new Exception('Failed to read buffer entry ' . $i);
35+
}
3136
$string .= chr($buffer[$i]);
3237
}
3338

@@ -68,6 +73,9 @@ public static function getBytes(array &$buffer, int $length, int &$offset = 0):
6873
{
6974
$datas = '';
7075
for ($i = $offset; $i < $offset + $length; $i++) {
76+
if (!isset($buffer[$i])) {
77+
throw new Exception('Failed to read buffer entry ' . $i);
78+
}
7179
$datas .= $buffer[$i];
7280
}
7381

@@ -89,6 +97,12 @@ public static function readBBO(array &$buffer, int $length, int &$offset = 0): i
8997
$len = $length / 2;
9098

9199
for ($i = 0; $i < $len; $i++) {
100+
if (!isset($buffer[$offset + ($len - 1 - $i)])) {
101+
throw new Exception('Failed to read buffer entry ' . ($offset + ($len - 1 - $i)));
102+
}
103+
if (!isset($buffer[$offset + $len + $i])) {
104+
throw new Exception('Failed to read buffer entry ' . ($offset + $len + $i));
105+
}
92106
$n1 += $buffer[$offset + ($len - 1 - $i)];
93107
$n2 += $buffer[$offset + $len + $i];
94108

@@ -115,6 +129,10 @@ public static function readLSB(array &$buffer, int $length, int &$offset = 0): i
115129
{
116130
$lsb = 0;
117131
for ($i = 0; $i < $length; $i++) {
132+
if (!isset($buffer[$offset + ($length - 1 - $i)])) {
133+
throw new Exception('Failed to read buffer entry ' . ($offset + ($length - 1 - $i)));
134+
}
135+
118136
$lsb += $buffer[$offset + $length - 1 - $i];
119137

120138
if ($i + 1 < $length) {
@@ -135,6 +153,9 @@ public static function readMSB(array &$buffer, int $length, int &$offset = 0): i
135153
{
136154
$msb = 0;
137155
for ($i = 0; $i < $length; $i++) {
156+
if (!isset($buffer[$offset + $i])) {
157+
throw new Exception('Failed to read buffer entry ' . ($offset + $i));
158+
}
138159
$msb += $buffer[$offset + $i];
139160

140161
if ($i + 1 < $length) {
@@ -155,6 +176,14 @@ public static function readInt16(array &$buffer, int &$offset = 0): int
155176
{
156177
$output = 0;
157178

179+
if (!isset($buffer[$offset + 0])) {
180+
throw new Exception('Failed to read buffer entry ' . ($offset + 0));
181+
}
182+
183+
if (!isset($buffer[$offset + 1])) {
184+
throw new Exception('Failed to read buffer entry ' . ($offset + 1));
185+
}
186+
158187
$output += $buffer[$offset + 0] << 8;
159188
$output += $buffer[$offset + 1];
160189

@@ -171,6 +200,22 @@ public static function readInt32(array &$buffer, int &$offset = 0): int
171200
{
172201
$output = 0;
173202

203+
if (!isset($buffer[$offset + 0])) {
204+
throw new Exception('Failed to read buffer entry ' . ($offset + 0));
205+
}
206+
207+
if (!isset($buffer[$offset + 1])) {
208+
throw new Exception('Failed to read buffer entry ' . ($offset + 1));
209+
}
210+
211+
if (!isset($buffer[$offset + 2])) {
212+
throw new Exception('Failed to read buffer entry ' . ($offset + 2));
213+
}
214+
215+
if (!isset($buffer[$offset + 3])) {
216+
throw new Exception('Failed to read buffer entry ' . ($offset + 3));
217+
}
218+
174219
$output += $buffer[$offset + 0] << 24;
175220
$output += $buffer[$offset + 1] << 16;
176221
$output += $buffer[$offset + 2] << 8;

tests/IsoFileTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public function testConstructorFileDoesNotExist(): void
1818
new IsoFile($testFile);
1919
}
2020

21-
// public function testConstructorInvalidFile(): void
22-
// {
23-
// $testFile = dirname(__FILE__, 2) . '/fixtures/invalid.iso';
24-
//
25-
// $this->expectException(Exception::class);
26-
// new IsoFile($testFile);
27-
// }
21+
public function testConstructorInvalidFile(): void
22+
{
23+
$testFile = dirname(__FILE__, 2) . '/fixtures/invalid.iso';
24+
25+
$this->expectException(Exception::class);
26+
new IsoFile($testFile);
27+
}
2828

2929
public function testConstructorExistingFile(): void
3030
{

0 commit comments

Comments
 (0)