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: continue parsing dictionnary object when error is detected #2872

Merged
merged 6 commits into from
Sep 27, 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
2 changes: 2 additions & 0 deletions pypdf/_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def strict(self) -> bool:


class PdfReaderProtocol(PdfCommonDocProtocol, Protocol):
strict: bool

@property
@abstractmethod
def xref(self) -> Dict[int, Dict[int, Any]]:
Expand Down
19 changes: 17 additions & 2 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
logger_warning,
read_non_whitespace,
read_until_regex,
read_until_whitespace,
skip_over_comment,
)
from ..constants import (
Expand Down Expand Up @@ -567,7 +568,17 @@ def read_unsized_from_stream(
break
stream.seek(-1, 1)
try:
key = read_object(stream, pdf)
try:
key = read_object(stream, pdf)
if not isinstance(key, NameObject):
raise PdfReadError(
f"Expecting a NameObject for key but found {key!r}"
)
except PdfReadError as exc:
if pdf is not None and pdf.strict:
raise
logger_warning(exc.__repr__(), __name__)
continue
tok = read_non_whitespace(stream)
stream.seek(-1, 1)
value = read_object(stream, pdf, forced_encoding)
Expand Down Expand Up @@ -1443,9 +1454,13 @@ def read_object(
else:
return NumberObject.read_from_stream(stream)
else:
pos = stream.tell()
stream.seek(-20, 1)
txt = stream.read(80)
stream.seek(pos)
read_until_whitespace(stream)
raise PdfReadError(
f"Invalid Elementary Object starting with {tok!r} @{stream.tell()}: {stream.read(80).__repr__()}"
f"Invalid Elementary Object starting with {tok!r} @{pos}: {txt!r}"
)


Expand Down
46 changes: 46 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,3 +1657,49 @@ def test_comments_in_array(caplog):
reader.stream = BytesIO(b[:1149])
with pytest.raises(PdfStreamError):
reader.pages[0]


@pytest.mark.enable_socket()
def test_space_in_names_to_continue_processing(caplog):
"""
This deals with space not encoded in names inducing errors
Also covers case where NameObject not met for key
"""
url = "https://github.com/user-attachments/files/17095516/crash-e108c4f677040b61e12fa9f1cfde025d704c9b0d.pdf"
name = "iss2866.pdf" # reused
b = get_data_from_url(url, name=name)
reader = PdfReader(BytesIO(b))
obj = reader.get_object(70)
assert all(
x in obj
for x in (
"/BaseFont",
"/DescendantFonts",
"/Encoding",
"/Subtype",
"/ToUnicode",
"/Type",
)
)
assert obj["/BaseFont"] == "/AASGAA+Arial,Unicode" # MS is missing to meet spec
assert 'PdfReadError("Invalid Elementary Object starting with' in caplog.text

caplog.clear()

b = b[:264] + b"(Inv) /d " + b[273:]
reader = PdfReader(BytesIO(b))
obj = reader.get_object(70)
assert all(
x in obj
for x in ["/DescendantFonts", "/Encoding", "/Subtype", "/ToUnicode", "/Type"]
)
assert all(
x in caplog.text
for x in (
"Expecting a NameObject for key but",
'PdfReadError("Invalid Elementary Object starting with',
)
)
reader = PdfReader(BytesIO(b), strict=True)
with pytest.raises(PdfReadError):
obj = reader.get_object(70)
Loading