From f008dce902e07e106e3eac39c61df1fecb201899 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Mon, 26 Oct 2026 10:28:17 -0400 Subject: [PATCH] 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 --- deeptutor/co_writer/docx_converter.py | 4 +++ tests/co_writer/__init__.py | 0 tests/co_writer/test_docx_inline_nul.py | 46 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/co_writer/__init__.py create mode 100644 tests/co_writer/test_docx_inline_nul.py diff --git a/deeptutor/co_writer/docx_converter.py b/deeptutor/co_writer/docx_converter.py index 4dd78dbb7..a5af7a877 100644 --- a/deeptutor/co_writer/docx_converter.py +++ b/deeptutor/co_writer/docx_converter.py @@ -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): diff --git a/tests/co_writer/__init__.py b/tests/co_writer/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/co_writer/test_docx_inline_nul.py b/tests/co_writer/test_docx_inline_nul.py new file mode 100644 index 000000000..ff505d417 --- /dev/null +++ b/tests/co_writer/test_docx_inline_nul.py @@ -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"