fix(co_writer): don't crash DOCX export on a literal NUL in Markdown

_add_inline_runs masks backslash escapes with the sentinel _MASK = '\x00' and
pops one saved character per sentinel in _unmask. A NUL already present in the
input collides with the sentinel but has no saved entry, so _unmask called
deque.popleft() on an empty deque and raised IndexError, aborting the entire
Word export. NUL is not valid XML text content, so strip it at the single
inline-text choke point before masking.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Rudy Celekli
2026-10-26 10:28:17 -04:00
parent a31d82624c
commit f008dce902
3 changed files with 50 additions and 0 deletions

View File

@@ -624,6 +624,10 @@ def _unmask(fragment: str, saved: deque[str]) -> str:
def _add_inline_runs(paragraph: Any, text: str) -> None:
# A literal NUL in the input collides with the escape sentinel ``_MASK``
# and would make ``_unmask`` pop a saved char that was never stored. NUL is
# not valid XML text content anyway, so drop it before masking.
text = text.replace(_MASK, "")
masked, saved = _mask_escapes(text)
cursor = 0
for match in _INLINE_RE.finditer(masked):

View File

View File

@@ -0,0 +1,46 @@
"""A literal NUL in Markdown must not crash the Word (DOCX) export.
``_add_inline_runs`` masks backslash escapes with the sentinel ``_MASK = "\x00"``
and pops one saved character per sentinel in ``_unmask``. A NUL already present
in the *input* collides with the sentinel but has no saved entry, so ``_unmask``
called ``deque.popleft()`` on an empty deque and raised ``IndexError`` — aborting
the whole export instead of treating the NUL as ordinary text.
"""
from __future__ import annotations
from unittest.mock import MagicMock
from deeptutor.co_writer.docx_converter import _add_inline_runs
class _FakeParagraph:
"""Minimal stand-in so the pure text path runs without python-docx."""
def __init__(self) -> None:
self.texts: list[str] = []
def add_run(self, text: str = "") -> MagicMock:
self.texts.append(text)
return MagicMock()
def test_literal_nul_in_markdown_does_not_crash_inline_runs() -> None:
paragraph = _FakeParagraph()
_add_inline_runs(paragraph, "Hello \x00 world")
rendered = "".join(paragraph.texts)
assert "\x00" not in rendered
assert rendered == "Hello world"
def test_nul_next_to_real_escape_is_handled() -> None:
paragraph = _FakeParagraph()
# ``\*`` is a real escape (masked then restored); the stray NUL must not
# steal its saved character.
_add_inline_runs(paragraph, "a\\*b\x00c")
rendered = "".join(paragraph.texts)
assert rendered == "a*bc"