smalot / smalot/pdfparser

Exception not handling on "Allowed memory size exhausted"

Open
#519 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
2.7k
Forks
579
Avg merge
2m
Merged PRs (30d)
1

Description

Hi,

I have the following exception :

Symfony\Component\ErrorHandler\Error\FatalError

  Allowed memory size of 536870912 bytes exhausted (tried to allocate 167772160 bytes)

  at vendor/smalot/pdfparser/src/Smalot/PdfParser/Font.php:221
    217▕                         $char_to = hexdec($matches['to'][$key]);
    218▕                         $offset = hexdec($matches['offset'][$key]);
    219▕
    220▕                         for ($char = $char_from; $char <= $char_to; ++$char) {
  ➜ 221▕                             $this->table[$char] = self::uchr($char - $char_from + $offset);
    222▕                         }
    223▕                     }
    224▕
    225▕                     // Support for : <srcCode1> <srcCodeN> [<dstString1> <dstString2> ... <dstStringN>]

I tried to handle it by a try / catch, but it's not working:

try {
    echo 'A';
    $pdf = $pdfParser->parseFile($pathname.'.pdf');
}
catch (\Whoops\Exception\ErrorException $e) {
    echo ' - Error in $pdf->parseFile() : '.$e->getMessage().' - ';
    return $new_document;
}
catch (\Symfony\Component\ErrorHandler\Error\FatalError $e) {
    echo ' - Error in $pdf->parseFile() : '.$e->getMessage().' - ';
    return $new_document;
}
catch (\Exception $e) {
    echo ' - Error in $pdf->parseFile() : '.$e->getMessage().' - ';
    return $new_document;
}
echo 'B';

The error is in parseFile() because the "A" is echoing, but not the "B".

Do you know why the exception is not handling?
Maybe it can be a solution to add a try / catch around the self::uchr ?

Thanks for your help and advice :-)

(If it could help, the parsed PDF is here : https://www.assemblee-nationale.fr/dyn/opendata/PIONANR5L15TAP0528.pdf)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at parseFile() and the reported failure in vendor/smalot/pdfparser/src/Smalot/PdfParser/Font.php:221, then reproduce it with the linked PDF. Check the existing error-handling path and determine whether this memory-exhaustion case is expected to be handled; done should be a documented behavior or a focused regression test and agreed change scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.