Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #621: Font::uchr: allow int|float for $code parameter #623

Merged
merged 7 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
k00ni marked this conversation as resolved.
Show resolved Hide resolved

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