Skip to content

Stack overflow error with apply_redactions and show_pdf_page #4469

@anthony-comp

Description

@anthony-comp

Description of the bug

I am creating a system that will insert text after specific elements on a pdf page.
It does the following operations in order:

  1. Search for insert location
  2. Based on text insertion size, determine the shift amount for content below insertion location
  3. Create a new page, extended vertically by shift amount
  4. Place the content above the insertion point at the top of the new page, and place the content below it low enough for the text to be written into the space created
  5. Write the text into the space created

In this process of copying over information to blank pages, show_pdf_page is used and redactions are used alongside it to prevent invisible data being carried over. When doing this sequence of events too many times (in my case over 8) on the same page (real pdf page, not pymupdf page) apply_redactions or show_pdf_page (depending on situations described below) will result in a stack overflow error.

How to reproduce the bug

On my account there is a repository called pymupdf-bug -> https://github.com/anthony-comp/pymupdf-bug
This contains python code and a sample pdf file that will produce the stack overflow issue.

Note that the stack overflow first occurs during apply_redactions() - if a try/except were to be wrapped around these and have them be ignored with pass or something similar, the stack overflow will eventually occur during show_pdf_page later on.

Note that if the loop_count variable is changed to be 8 or lower, the bug will not occur and the desired output will be generated.

Here is the output:

MuPDF error: exception stack overflow!

Traceback (most recent call last):
  File "C:\Users\USERNAME\bug_report.py", line 157, in <module>
    main()
  File "C:\Users\USERNAME\bug_report.py", line 132, in main
    above_page.apply_redactions()
  File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python310\lib\site-packages\pymupdf\utils.py", line 4241, in apply_redactions
    rc = page._apply_redactions(text, images, graphics)  # call MuPDF
  File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python310\lib\site-packages\pymupdf\__init__.py", line 7836, in _apply_redactions
    success = mupdf.pdf_redact_page(page.doc(), page, opts)
  File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python310\lib\site-packages\pymupdf\mupdf.py", line 53155, in pdf_redact_page
    return _mupdf.pdf_redact_page(doc, page, opts)
pymupdf.mupdf.FzErrorLimit: code=5: exception stack overflow!

PyMuPDF version

1.25.5

Operating system

Windows

Python version

3.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions