Skip to content

Commit

Permalink
ROB: Raise PdfReadError when missing /Root in trailer (#2808)
Browse files Browse the repository at this point in the history
Fixes #2806.
  • Loading branch information
BertrandBordage authored Aug 23, 2024
1 parent d2d520b commit 9f08cd0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def close(self) -> None:
@property
def root_object(self) -> DictionaryObject:
"""Provide access to "/Root". Standardized with PdfWriter."""
return cast(DictionaryObject, self.trailer[TK.ROOT].get_object())
root = self.trailer[TK.ROOT]
if root is None:
raise PdfReadError('Cannot find "/Root" key in trailer')
return cast(DictionaryObject, root.get_object())

@property
def _info(self) -> Optional[DictionaryObject]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,9 @@ def test_read_unknown_zero_pages(caplog):
"startxref on same line as offset",
]
assert normalize_warnings(caplog.text) == warnings
with pytest.raises(AttributeError) as exc:
with pytest.raises(PdfReadError) as exc:
len(reader.pages)
assert exc.value.args[0] == "'NoneType' object has no attribute 'get_object'"
assert exc.value.args[0] == 'Cannot find "/Root" key in trailer'


def test_read_encrypted_without_decryption():
Expand Down

0 comments on commit 9f08cd0

Please sign in to comment.