Skip to content

Commit

Permalink
Fixes #621: Font::uchr: allow int|float for $code parameter (#623)
Browse files Browse the repository at this point in the history
* Font::uchr: extends parameter $code to int|float

* Add files via upload

* fixed int|float so code runs on PHP 7.x

* add test case

* fixed coding style issue in Font.php

* added note

* Update Font.php
  • Loading branch information
k00ni authored Aug 3, 2023
1 parent ce434c1 commit c974994
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Binary file added samples/bugs/Issue621.pdf
Binary file not shown.
9 changes: 8 additions & 1 deletion src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,16 @@ public function translateChar(string $char, bool $use_default = true)

/**
* Convert unicode character code to "utf-8" encoded string.
*
* @param int|float $code Unicode character code. Will be casted to int internally!
*/
public static function uchr(int $code): string
public static function uchr($code): string
{
// note:
// $code was typed as int before, but changed in https://github.com/smalot/pdfparser/pull/623
// because in some cases uchr was called with a float instead of an integer.
$code = (int) $code;

if (!isset(self::$uchrCache[$code])) {
// html_entity_decode() will not work with UTF-16 or UTF-32 char entities,
// therefore, we use mb_convert_encoding() instead
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPUnit/Integration/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ public function testIssue557(): void
);
}

/**
* Tests if an integer overflow triggers a TypeError in Font::uchr.
*
* @see https://github.com/smalot/pdfparser/issues/621
*/
public function testIssue621(): void
{
$document = $this->fixture->parseFile($this->rootDir.'/samples/bugs/Issue621.pdf');

$this->assertStringContainsString('What is a biological product?', $document->getText());
}

/**
* Tests behavior when changing default font space limit (-50).
*
Expand Down

0 comments on commit c974994

Please sign in to comment.