Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion pypdf/_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,14 @@ def get_fields(
return retval
assert stack is not None
if "/Fields" in tree:
fields = cast(ArrayObject, tree["/Fields"])
fields = tree["/Fields"].get_object()
if not isinstance(fields, ArrayObject):
logger_warning(
"AcroForm /Fields is not an array: %(fields)s",
source=__name__,
fields=fields,
)
return retval
for f in fields:
field = f.get_object()
self._build_field(field, retval, fileobj, field_attributes, stack)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,32 @@ def test_get_outline__entry_count():
expected_exception=LimitReachedError, match=r"^Maximum outline entry limit reached: 1001 > 1000\.$"
):
writer._get_outline()


@pytest.mark.parametrize(
("fields", "expected"),
[
pytest.param(
NumberObject(1), "AcroForm /Fields is not an array: 1", id="number"
),
pytest.param(
TextStringObject("x"), "AcroForm /Fields is not an array: x", id="string"
),
pytest.param(
DictionaryObject(), "AcroForm /Fields is not an array: {}", id="dictionary"
),
],
)
Comment thread
stefan6419846 marked this conversation as resolved.
Resolved
def test_get_fields__fields_is_not_an_array(caplog, fields, expected):
"""A malformed /Fields raised a TypeError when the loop tried to iterate it."""
writer = PdfWriter()
writer.add_blank_page(width=72, height=72)
writer.root_object[NameObject("/AcroForm")] = DictionaryObject(
{NameObject("/Fields"): fields}
)
stream = BytesIO()
writer.write(stream)
stream.seek(0)

assert PdfReader(stream).get_fields() == {}
assert expected in caplog.text
Loading