Repository navigation
POIFSStream: clear freed blocks so deleted data isn't written out - #1520
Open
naruto-lgtm wants to merge 1 commit into
Open
naruto-lgtm wants to merge 1 commit into
naruto-lgtm wants to merge 1 commit into
Conversation
POIFSStream.free only marked the blocks of a deleted or replaced stream as unused and left their contents, while writeFilesystem() and writeFilesystem(OutputStream) write the whole data source, free blocks included. Deleting an entry, replacing it with shorter data, or removing a sheet and saving an .xls in place therefore kept the old bytes in the saved file. Zero each block before it is marked unused. Blocks that aren't backed by data are skipped, and file systems that aren't modified are written as before.
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.
Repro: write a POIFS with a 10,000-byte stream, reopen it, delete the stream (or replace it with 10 bytes through
createOrUpdateDocument) and callwriteFilesystem(OutputStream): every byte of the old stream is still in the output. The in-place path behaves the same, e.g. opening an .xls read-write,removeSheetAtandHSSFWorkbook.write()leaves all cell records of the removed sheet in the file.Cause:
POIFSStream.freeonly sets the FAT/SBAT entries to unused and leaves the block contents, whilewriteFilesystem()andwriteFilesystem(OutputStream)write the whole data source, free blocks included (CWE-226). Every path that gives up stream space goes through it:DirectoryEntry.delete,POIFSDocument.replaceContents(behindcreateOrUpdateDocument, the HPSF property writers and the in-placewrite()of HSSF, HSLF and HWPF) andDocumentOutputStream.Fix: zero each big or mini block in
free()before marking it unused, the same state POIFS gives newly allocated blocks.HSSFWorkbook.write(OutputStream)andwrite(File)build a new file system and were never affected; in-place saves and direct POIFS edits are. The cost is one extra write per released block, the same order as writing the replacement data. File systems that aren't modified are still written byte for byte as before, verified against a few Office-written samples. Blocks that aren't backed by data (a truncated file) are skipped, so deleting entries of such files keeps working.One related leftover is out of scope here: when the directory shrinks, the 128-byte entry of a deleted stream (its name, not its data) can stay in the unused tail of the last directory sector. Clearing that tail also rewrote the free directory entries of unmodified Office files, so I left it for a separate change if you want it.
TestPOIFSStream.deletedDataIsNotWrittenOutdeletes a big-block and a mini-stream entry and shrinks a third, throughwriteFilesystem(OutputStream)and in place, and fails before the change.:poi,:poi-scratchpad,:poi-ooxmland:poi-integrationpass locally apart fromTestDateSerialRange.testEdgesOfTheRangeand three SXSSF/XSSF auto-size width tests, which fail the same way on trunk here.