From 9f08cd0e48114b5788e9c219b443bf75dcdbe251 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Fri, 23 Aug 2024 07:43:06 +0200 Subject: [PATCH] ROB: Raise PdfReadError when missing /Root in trailer (#2808) Fixes #2806. --- pypdf/_reader.py | 5 ++++- tests/test_reader.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pypdf/_reader.py b/pypdf/_reader.py index 1ffcd436d..1452661a5 100644 --- a/pypdf/_reader.py +++ b/pypdf/_reader.py @@ -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]: diff --git a/tests/test_reader.py b/tests/test_reader.py index 0413a9135..d2394f95d 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -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():