Skip to content

Commit

Permalink
ROB: Accept empty object as null objects (#1477)
Browse files Browse the repository at this point in the history
Fixes #1474
  • Loading branch information
pubpub-zz committed Dec 8, 2022
1 parent 635f0ca commit 7f586ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion PyPDF2/generic/_data_structures.py
Expand Up @@ -847,6 +847,9 @@ def read_object(
return BooleanObject.read_from_stream(stream)
elif tok == b"(":
return read_string_from_stream(stream, forced_encoding)
elif tok == b"e" and stream.read(6) == b"endobj":
stream.seek(-6, 1)
return NullObject()
elif tok == b"n":
return NullObject.read_from_stream(stream)
elif tok == b"%":
Expand All @@ -869,7 +872,7 @@ def read_object(
else:
return NumberObject.read_from_stream(stream)
else:
stream.read(-20)
stream.seek(-20, 1)
raise PdfReadError(
f"Invalid Elementary Object starting with {tok!r} @{stream.tell()}: {stream.read(80).__repr__()}"
)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_generic.py
Expand Up @@ -292,6 +292,20 @@ def test_read_object_comment_exception():
assert exc.value.args[0] == "File ended unexpectedly."


def test_read_object_empty():
stream = BytesIO(b"endobj")
pdf = None
assert isinstance(read_object(stream, pdf), NullObject)


def test_read_object_invalid():
stream = BytesIO(b"hello")
pdf = None
with pytest.raises(PdfReadError) as exc:
read_object(stream, pdf)
assert "hello" in exc.value.args[0]


def test_read_object_comment():
stream = BytesIO(b"% foobar\n1 ")
pdf = None
Expand Down

0 comments on commit 7f586ae

Please sign in to comment.