fix(docx): avoid panic on save, make AnyDocument.save() extension-aware#88
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DocxDocument::save_to_bytesround-tripped throughdocx-rs's writer, which panics inBuildXML for RunChildonRunChild::InstrTextString(reader-only variant, writer arm isunreachable!()). Any DOCX withPAGE,NUMPAGES,PAGEREF,TOC, orHYPERLINKfields crasheddoc.save(...). Upstream issue: bokuweb/docx-rs#750, still open; introduced by PR #528. Fix: return the stashed input bytes —paperjam-docxexposes no DOCX mutation API, so rebuilding was both lossy and panic-prone for no benefit.AnyDocument.save(path)ignored the target extension.doc.save("out.pdf")on a DOCX wrote DOCX bytes into a.pdffile. Now routes throughconvert_to(ext)when the target extension differs fromself.format; otherwise writes original bytes unchanged.Repro (panic + wrong-format file) from the bug report:
After this PR: produces a valid 62-page PDF (
%PDF-1.7).Test plan
uv run maturin develop --release— builds cleanCurriculumGuideGibbsHighSchool.docx— no panic, output opens as a valid PDF withpage_count == 62and extractable textcargo test --workspace/pytest tests/python/should remain green (no behavior change for same-format saves; only adds cross-format routing)Notes / follow-ups
inner.build().pack()becomes meaningful.save(path)UX gap exists on the PDFDocument.save()path (writes PDF regardless of extension). Out of scope here; can address in a follow-up if desired.