Skip to content

IndexError: pop from empty list in read_fld_char (orphaned fldChar end/separate) #172

Closed

Description

@GokouRuri43

Problem

Converting a DOCX that contains a Word field marker (w:fldChar) whose w:fldCharType is end or separate without a matching begin crashes with IndexError: pop from empty list in read_fld_char (mammoth/docx/body_xml.py). The complex-field stack is empty, so .pop() raises and the whole conversion fails.

Reproduction

A paragraph with an orphaned field end:

element = xml_element("w:p", {}, [
    _run_element_with_text("some text"),
    xml_element("w:r", {}, [xml_element("w:fldChar", {"w:fldCharType": "end"})]),
])
_read_and_get_document_xml_element(element)  # IndexError: pop from empty list

It also reproduces on a real document: 090716_Studentische_Arbeit_VWS.docx from the LibreOffice core test data (MPL-2.0):
https://github.com/LibreOffice/core/blob/master/sw/qa/extras/ooxmlexport/data/090716_Studentische_Arbeit_VWS.docx

Fix

Guard both pop sites against an empty stack, treating an orphaned end/separate marker as an empty result:

elif fld_char_type == "end":
    if not complex_field_stack:
        return _empty_result
    complex_field = complex_field_stack.pop()
    ...

elif fld_char_type == "separate":
    if not complex_field_stack:
        return _empty_result
    ...

Tests

Added test_fld_char_end_without_matching_begin_is_ignored to tests/docx/body_xml_tests.py. It fails on the current code with IndexError: pop from empty list and passes with the fix. The full tests/docx/body_xml_tests.py passes (157 tests).

Note: tests/cli_tests.py and tests/docx/files_tests.py have pre-existing failures on Windows (POSIX /tmp path expectations) that are unrelated to this change.

Activity

  1. mwilliamson commented on Sep 9, 2026

    @mwilliamson
    Owner

    Is this not a duplicate of #168?

  2. GokouRuri43 commented on Sep 9, 2026

    @GokouRuri43
    Author

    Yes — this is a duplicate of #168. Apologies for the noise; I searched markitdown and pdfminer but missed this repository's own issue tracker. Closing this one and moving the marginal value I have (a real-world reproduction file and a ready regression test) over to #168.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions