Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB: Tolerate "truncated" xref #2580

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ def _read_standard_xref_table(self, stream: StreamType) -> None:
read_non_whitespace(stream)
stream.seek(-1, 1)
size = cast(int, read_object(stream, self))
if not isinstance(size, int):
logger_warning(
"Invalid/Truncated xref table. Rebuild xref table",
__name__,
)
self._rebuild_xref_table(stream)
stream.read()
return
read_non_whitespace(stream)
stream.seek(-1, 1)
cnt = 0
Expand Down Expand Up @@ -815,6 +823,9 @@ def _read_xref_tables_and_trailers(

def _read_xref(self, stream: StreamType) -> Optional[int]:
self._read_standard_xref_table(stream)
if stream.read(1) == b"":
return None
stream.seek(-1, 1)
read_non_whitespace(stream)
stream.seek(-1, 1)
new_trailer = cast(Dict[str, Any], read_object(stream, self))
Expand Down
8 changes: 8 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,3 +1508,11 @@ def test_corrupted_xref():
name = "iss2516.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
assert reader.root_object["/Type"] == "/Catalog"


@pytest.mark.enable_socket()
def test_truncated_xref(caplog):
url = "https://github.com/py-pdf/pypdf/files/14843553/002-trivial-libre-office-writer-broken.pdf"
name = "iss2575.pdf"
PdfReader(BytesIO(get_data_from_url(url, name=name)))
assert "Invalid/Truncated xref table. Rebuild xref table" in caplog.text
Loading