Skip to content
Open
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
13 changes: 12 additions & 1 deletion pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,18 @@ def read_from_stream(
stream.seek(-len(rest), 1)
stream.seek(-1, 1)
# read and append object
arr.append(read_object(stream, pdf, forced_encoding, parsing_budget=parsing_budget))
try:
obj = read_object(stream, pdf, forced_encoding, parsing_budget=parsing_budget)
except LimitReachedError:
# Always propagate security issues.
raise
except (MemoryError, RecursionError) as exc:
# A deeply nested array (e.g. "[[[[...]]]]") blows the interpreter's

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this large comment? The referenced case does not have one and nesting triggering recursion errors in such a scenario should be obvious and - if required - derivable from the corresponding PR once merged.

# recursion limit before the element-count budget above ever triggers,
# since nesting depth, not element count, drives the recursion here.
# Mirrors the guard DictionaryObject.read_from_stream already applies.
raise PdfReadError("Error reading PDF file.") from exc
arr.append(obj)
return arr


Expand Down
11 changes: 11 additions & 0 deletions tests/generic/test_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,17 @@ def test_array_object__read_from_stream__maximum_element_count() -> None:
ArrayObject.read_from_stream(BytesIO(data), pdf=pdf) # type: ignore[arg-type]


def test_array_object__read_from_stream__deeply_nested_array_raises_pdfreaderror() -> None:
# A single array nested thousands of levels deep drives recursion depth,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see how this comment provides benefits, especially as inline comment instead of docstring.

# not element count, so array_maximum_element_count never engages.
# Previously this raised an uncaught RecursionError instead of PdfReadError.
pdf = ReaderDummy()
depth = 5000

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a depth of 5000 to trigger an issue which occurs with exceeding sys.getrecursionlimit(), which usually is just 1000?

data = b"[" * depth + b"]" * depth
with pytest.raises(PdfReadError, match=r"^Error reading PDF file\.$"):
ArrayObject.read_from_stream(BytesIO(data), pdf=pdf) # type: ignore[arg-type]


def test_dictionary_object__read_from_stream__token_limit() -> None:
pdf = ReaderDummy()
data = b"<< /Key1 << /Key2 << /Key3 << /Key4 << /SomeName 42 >> >> >> >> /Key5 13 >>"
Expand Down
Loading