perf(odt): stream unprocessed package entries, bound XML part sizes, truncate seekable outputs - #221
Merged
Merged
Conversation
…em, bound XML part sizes, truncate seekable outputs (#138) - OdtPackage inflates only mimetype, content.xml, styles.xml, meta.xml and the manifest (each at most 256 MB uncompressed, at most 65,535 entries); all other entries are streamed from the source archive at save time. A 100 KB template with a 96 MB entry allocated ~353 MB before and ~1.3 MB now. - Save cuts off a seekable output after the written package (File.OpenWrite on a longer existing file no longer leaves trailing bytes). - DocumentTemplateProcessor truncates the output after copying the template, so an existing longer file opened with FileMode.OpenOrCreate no longer fails as corrupted.
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Sep 26, 2026
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.
Problem
Review finding #4 (MEDIUM):
OdtPackage.Openinflated every ZIP entry into abyte[], andSavebuilt a second full copy in aMemoryStream, with no bound on decompression. A 100 KB template with a 96 MB zero-filledPictures/big.binallocated about 353 MB.Review finding #2 (code part):
Savenever truncated a seekable output. Writing into an existing, longer file opened withFile.OpenWriteleft the old trailing bytes behind, and the file could not be read back.Fix
OdtPackagenow inflates onlymimetype,content.xml,styles.xml,meta.xmlandMETA-INF/manifest.xml. All other entries are streamed at save time from the source archive entry into the output entry, so memory is bounded by the copy buffer. A non-seekable template is buffered once, in compressed form.Invalid document: content.xml exceeds the maximum supported size (256 MB uncompressed).mimetypecomes first, stored, with no extra field and no data descriptor;.ottbecomes.odt; signatures are removed; output is UTF-8 without BOM.Savecuts off a seekable output after the written package (SetLength(Position)).DocumentTemplateProcessorrejectsFile.OpenWrite(write-only) up front. With an existing, longer file openedReadWritewithout truncation (FileMode.OpenOrCreate), the trailing bytes made the package unreadable, and processing failed with "File contains corrupted data". It now truncates the output after the template copy. This is internal, and it only turns a failing case into a working one.docs/for-developers/opendocument.md, plus the spec and the XML doc of the output stream parameter.Tests
GC.GetAllocatedBytesForCurrentThread): before 369,863,784 bytes (~353 MB), after 1,361,352 bytes (~1.3 MB).OdtPackageTests:Process_LargeUnprocessedEntry_IsStreamedWithoutInflatingIntoMemoryandProcess_NonSeekableTemplateWithLargeEntry_BuffersOnlyCompressedData: a 64 MB entry must allocate less than 1/4 of its size; the entry is copied intact and the output stays compressed.Open_XmlPartLargerThanLimit_ThrowsInvalidPackageProcess_ExistingLongerOutputFile_IsTruncated: the review probeDocs_FileOpenWrite_OnExistingLargerFile_LeavesTrailingBytes, flipped.Process_SeekableOutputWithPrefix_KeepsPrefixAndCutsOffTheRestStreamValidationTests.ProcessTemplate_ExistingLongerOutputFile_IsTruncated(DOCX).-p:ContinuousIntegrationBuild=true; all tests pass on net10/net9/net8 (2,097 each), including the LibreOffice-trait tests run locally (7/7).dotnet format --verify-no-changesanddotnet packpass.Public API impact
None. No public signatures changed. Behavior change: a seekable output stream is truncated after the written document, for ODT and for DOCX. Before, the cases this affects left a corrupt file (ODT) or failed (DOCX).