Skip to content

Commit

Permalink
Fixed bug in handling faulty pdf dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianKresse committed Nov 27, 2024
1 parent a3bf630 commit 866a255
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/PdfParser/Type/PdfDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ public static function parse(Tokenizer $tokenizer, StreamReader $streamReader, P
if (!($key instanceof PdfName)) {
$lastToken = null;
// ignore all other entries and search for the closing brackets
while (($token = $tokenizer->getNextToken()) !== '>' && $token !== false && $lastToken !== '>') {
while (($token = $tokenizer->getNextToken()) !== '>' || $lastToken !== '>') {
if ($token === false) {
return false;
}
$lastToken = $token;
}

if ($token === false) {
return false;
}

break;
}

Expand Down
1 change: 0 additions & 1 deletion src/PdfReader/PdfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public function getPageCount()
*/
public function getPage($pageNumber)
{
/** @phpstan-ignore function.alreadyNarrowedType */
if (!\is_numeric($pageNumber)) {
throw new \InvalidArgumentException(
'Page number needs to be a number.'
Expand Down
15 changes: 14 additions & 1 deletion tests/functional/PdfParser/Type/PdfDictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function parseProvider()
'A' => PdfString::create('hello')
])
],
[
'/A (hello) B [<>]>>',
PdfDictionary::create([
'A' => PdfString::create('hello')
])
],
// this faulty structure is currently not handled
// [
// '/A (hello) B [<<>>]>>',
// PdfDictionary::create([
// 'A' => PdfString::create('hello')
// ])
// ],
[
'/A (1) /B (2) /C <</D (4) /E (5)>> >>',
PdfDictionary::create([
Expand Down Expand Up @@ -120,8 +133,8 @@ public function testParse($in, $expectedResult)
$result = PdfDictionary::parse($tokenizer, $stream, $parser);

$this->assertInstanceOf(PdfDictionary::class, $result);

$this->assertEquals($expectedResult->value, $result->value);
$this->assertFalse($tokenizer->getNextToken());
}

public function testParseBehind()
Expand Down

0 comments on commit 866a255

Please sign in to comment.