Skip to content

Commit e92b20e

Browse files
farjasjujules
andauthored
BUG: Fix UnboundLocalError on malformed pdf (#2619)
Closes #2617 Co-authored-by: jules <[email protected]>
1 parent d9bf67f commit e92b20e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pypdf/_reader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ def get_object(
398398
self.stream.seek(m.start(0) + 1)
399399
idnum, generation = self.read_object_header(self.stream)
400400
else:
401-
idnum = -1 # exception will be raised below
401+
idnum = -1
402+
generation = -1 # exception will be raised below
402403
if idnum != indirect_reference.idnum and self.xref_index:
403404
# Xref table probably had bad indexes due to not being zero-indexed
404405
if self.strict:

tests/test_reader.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,3 +1516,17 @@ def test_truncated_xref(caplog):
15161516
name = "iss2575.pdf"
15171517
PdfReader(BytesIO(get_data_from_url(url, name=name)))
15181518
assert "Invalid/Truncated xref table. Rebuilding it." in caplog.text
1519+
1520+
1521+
@pytest.mark.enable_socket()
1522+
def test_damaged_pdf():
1523+
url = "https://github.com/py-pdf/pypdf/files/15186107/malformed_pdf.pdf"
1524+
name = "malformed_pdf.pdf"
1525+
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)), strict=False)
1526+
len(reader.pages)
1527+
strict_reader = PdfReader(BytesIO(get_data_from_url(url, name=name)), strict=True)
1528+
with pytest.raises(PdfReadError) as exc:
1529+
len(strict_reader.pages)
1530+
assert (
1531+
exc.value.args[0] == "Expected object ID (21 0) does not match actual (-1 -1)."
1532+
)

0 commit comments

Comments
 (0)