Skip to content

Commit c974994

Browse files
authoredAug 3, 2023
Fixes #621: Font::uchr: allow int|float for $code parameter (#623)
* 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
1 parent ce434c1 commit c974994

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
 

‎samples/bugs/Issue621.pdf

335 KB
Binary file not shown.

‎src/Smalot/PdfParser/Font.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,16 @@ public function translateChar(string $char, bool $use_default = true)
134134

135135
/**
136136
* Convert unicode character code to "utf-8" encoded string.
137+
*
138+
* @param int|float $code Unicode character code. Will be casted to int internally!
137139
*/
138-
public static function uchr(int $code): string
140+
public static function uchr($code): string
139141
{
142+
// note:
143+
// $code was typed as int before, but changed in https://github.com/smalot/pdfparser/pull/623
144+
// because in some cases uchr was called with a float instead of an integer.
145+
$code = (int) $code;
146+
140147
if (!isset(self::$uchrCache[$code])) {
141148
// html_entity_decode() will not work with UTF-16 or UTF-32 char entities,
142149
// therefore, we use mb_convert_encoding() instead

‎tests/PHPUnit/Integration/ParserTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ public function testIssue557(): void
294294
);
295295
}
296296

297+
/**
298+
* Tests if an integer overflow triggers a TypeError in Font::uchr.
299+
*
300+
* @see https://github.com/smalot/pdfparser/issues/621
301+
*/
302+
public function testIssue621(): void
303+
{
304+
$document = $this->fixture->parseFile($this->rootDir.'/samples/bugs/Issue621.pdf');
305+
306+
$this->assertStringContainsString('What is a biological product?', $document->getText());
307+
}
308+
297309
/**
298310
* Tests behavior when changing default font space limit (-50).
299311
*

0 commit comments

Comments
 (0)
Please sign in to comment.