Skip to content

Commit fd37d13

Browse files
committed
fix: check $pos before decode
closed: #13
1 parent e6b90a0 commit fd37d13

File tree

6 files changed

+18
-15
lines changed

6 files changed

+18
-15
lines changed

src/Bencode.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ public static function decode($data, &$pos = 0)
5656
{
5757
$start_decode = ($pos === 0); // If it is the root call ?
5858
if ($start_decode && (!is_string($data) || strlen($data) == 0)) {
59-
throw new ParseException('Decode Input is not valid String.');
59+
throw new ParseException('Decode Input is not valid String');
60+
}
61+
62+
if ($pos >= strlen($data)) {
63+
throw new ParseException('Unterminated bencode string literal');
6064
}
6165

6266
if ($data[$pos] === 'd') {
@@ -69,7 +73,7 @@ public static function decode($data, &$pos = 0)
6973
break;
7074
}
7175
if (!is_string($key)) {
72-
throw new ParseException('Non string key found in the dictionary.');
76+
throw new ParseException('Non string key found in the dictionary');
7377
} elseif (array_key_exists($key, $return)) {
7478
throw new ParseException('Duplicate Dictionary key exist before: ' . $key);
7579
}

src/TorrentFile.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class TorrentFile
5353
protected static function checkTorrentDict($dict, $key, $type = null)
5454
{
5555
if (!is_array($dict)) {
56-
throw new ParseException('Checking non-dictionary value.');
56+
throw new ParseException('Checking non-dictionary value');
5757
}
5858

5959
if (!isset($dict[$key])) {
@@ -578,7 +578,7 @@ public function parse()
578578
if ($this->getProtocol() === self::PROTOCOL_V1) { // Do what we do in protocol v1
579579
$pieces = self::checkTorrentDict($info, 'pieces', 'string');
580580
if (strlen($pieces) % 20 != 0) {
581-
throw new ParseException('Invalid pieces length.');
581+
throw new ParseException('Invalid pieces length');
582582
}
583583

584584
if ($this->getFileMode() === self::FILEMODE_SINGLE) {
@@ -596,7 +596,7 @@ public function parse()
596596

597597
foreach ($paths as $path) {
598598
if (!is_string($path)) {
599-
throw new ParseException('Invalid path with non-string value.');
599+
throw new ParseException('Invalid path with non-string value');
600600
}
601601
}
602602

@@ -635,15 +635,14 @@ public function parse()
635635
$length = self::checkTorrentDict($file, 'length', 'integer');
636636
if ($length > $pieceLength) { // check pieces root of large file is exist in $root['picec layers'] or not
637637
if (!array_key_exists($piecesRoot, $pieceLayers)) {
638-
throw new ParseException('Pieces not exist in piece layers.');
638+
throw new ParseException('Pieces not exist in piece layers');
639639
}
640640
}
641641

642642
$addFile($paths, $length);
643643
$merkleTree = $length; // rewrite merkleTree to size, it's safe since it not affect $data['info']['file tree']
644644
} else {
645645
$parent_path = $paths; // store parent paths
646-
/** @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */
647646
foreach ($merkleTree as $k => &$v) { // Loop tree
648647
$paths[] = $k; // push current path into paths
649648
$loopMerkleTree($v, $paths); // Loop check

tests/DecodeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function testDecodeDictionarySorted()
218218
public function testDecodeDictionaryKeyNotString()
219219
{
220220
$this->expectException(ParseException::class);
221-
$this->expectExceptionMessage('Non string key found in the dictionary.');
221+
$this->expectExceptionMessage('Non string key found in the dictionary');
222222

223223
Bencode::decode('di123ei321ee');
224224
}
@@ -263,7 +263,7 @@ public function testDecodeTorrent()
263263
public function testDecodeNothing()
264264
{
265265
$this->expectException(ParseException::class);
266-
$this->expectExceptionMessage('Decode Input is not valid String.');
266+
$this->expectExceptionMessage('Decode Input is not valid String');
267267

268268
Bencode::decode('');
269269
}
@@ -274,7 +274,7 @@ public function testDecodeNothing()
274274
public function testDecodeNull()
275275
{
276276
$this->expectException(ParseException::class);
277-
$this->expectExceptionMessage('Decode Input is not valid String.');
277+
$this->expectExceptionMessage('Decode Input is not valid String');
278278

279279
Bencode::decode(null);
280280
}

tests/TorrentV1MultiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testFilesPathNotArray() {
6060

6161
public function testFilesPathEntityNotString() {
6262
$this->expectException(ParseException::class);
63-
$this->expectExceptionMessage('Invalid path with non-string value.');
63+
$this->expectExceptionMessage('Invalid path with non-string value');
6464

6565
$files = $this->torrent->getInfoField('files');
6666
$files[0]['path'][0] = 123;

tests/traits/TorrentFileV1Trait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ trait TorrentFileV1Trait
1010

1111
public function testV1WithWrongPieces() {
1212
$this->expectException(ParseException::class);
13-
$this->expectExceptionMessage('Invalid pieces length.');
13+
$this->expectExceptionMessage('Invalid pieces length');
1414

1515
$this->torrent->setInfoField('pieces', $this->torrent->getRootField('pieces') . 'somestring');
1616
$this->torrent->parse();
1717
}
18-
}
18+
}

tests/traits/TorrentFileV2Trait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public function testInvalidNodeLength() {
6060

6161
public function testNodePiecesRootNotExistInPieceLayer() {
6262
$this->expectException(ParseException::class);
63-
$this->expectExceptionMessage('Pieces not exist in piece layers.');
63+
$this->expectExceptionMessage('Pieces not exist in piece layers');
6464

6565
$fileTree = $this->torrent->getInfoField('file tree');
6666
$fileTree['file1.dat']['']['pieces root'] = hash('sha256', 'adfadsfasd',true);
6767

6868
$this->torrent->setInfoField('file tree', $fileTree);
6969
$this->torrent->parse();
7070
}
71-
}
71+
}

0 commit comments

Comments
 (0)