Skip to content

FzErrorFormat: code=7: cycle in resources raised on page.rect for PDF with self-referential /Parent pointer #4986

@ReasunR

Description

@ReasunR

Description of the bug

PyMuPDF raises pymupdf.mupdf.FzErrorFormat: code=7: cycle in resources when accessing page.rect (or any other page operation) on a PDF where a page object's /Parent entry points to the page itself (instead of to a /Pages node).

Concretely, for the attached file:

  • Catalog /Pages points to xref 3 (the real /Pages root).
  • Page 0 lives at xref 4 and declares /Parent 4 0 R — i.e. itself.
  • Pages 1–7 also declare /Parent 4 0 R (so they inherit through the malformed page rather than through the real /Pages root).

repro_cycle_in_resources.pdf

When MuPDF inherits page resources by walking up the parent chain, it loops on page 0 → page 0 → … and aborts with cycle in resources. The error fires immediately on pdf_document[0].rect and on page.cropbox, so neither of the workarounds suggested in #2300 (using cropbox instead of rect) nor in #2749 (using insert_pdf to copy the page into a fresh document) works for this defect class. Document.tobytes(garbage=4, clean=True, deflate=True) also preserves the malformed graph and does not break the cycle.

To Reproduce

import fitz
doc = fitz.open("repro_cycle_in_resources.pdf")
print(f"Pages: {len(doc)}")
print(doc[0].rect)  # raises FzErrorFormat: code=7: cycle in resources

Output:

Traceback (most recent call last):
  File "...", line 4, in <module>
    print(doc[0].rect)
  File ".../pymupdf/__init__.py", line 10865, in bound
    val = mupdf.fz_bound_page(page)
  File ".../pymupdf/mupdf.py", line 45171, in fz_bound_page
    return _mupdf.fz_bound_page(page)
pymupdf.mupdf.FzErrorFormat: code=7: cycle in resources

My configuration:

Python 3.13
PyMuPDF 1.27.1: Python bindings for the MuPDF 1.27.1 library.
Issues happen on both MacOs arm64 and Linux x86_64.

Attached pdf:

Anonymized 1-file (8-page with anonymized content) reproducer attached: repro_cycle_in_resources.pdf (227 KB). Original file contained personal data; all text was replaced with non-existent glyph indices, all images blanked to 1×1 white, metadata wiped — but the malformed /Parent graph was preserved verbatim.

How to reproduce the bug

repro_cycle_in_resources.pdf

PyMuPDF version

1.27.1

Operating system

Linux

Python version

3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions