This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 81ab52621c POIFSStream: clear freed blocks so deleted data isn't
written out (#1520)
81ab52621c is described below
commit 81ab52621cb3ecaf9cb96a5a92edcb2e5fd60408
Author: Nayyar <[email protected]>
AuthorDate: Tue Nov 3 00:28:49 2026 +0530
POIFSStream: clear freed blocks so deleted data isn't written out (#1520)
clear freed POIFS blocks so deleted data isn't written out
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.
---
.../apache/poi/poifs/filesystem/POIFSStream.java | 22 ++++++-
.../poi/poifs/filesystem/TestPOIFSStream.java | 74 ++++++++++++++++++++++
2 files changed, 95 insertions(+), 1 deletion(-)
diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSStream.java
b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSStream.java
index 43d6b3b32c..e2a8073356 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSStream.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSStream.java
@@ -135,17 +135,37 @@ public class POIFSStream implements Iterable<ByteBuffer>
ChainLoopDetector loopDetector = blockStore.getChainLoopDetector();
free(loopDetector);
}
- private void free(ChainLoopDetector loopDetector) {
+ private void free(ChainLoopDetector loopDetector) throws IOException {
int nextBlock = startBlock;
while(nextBlock != POIFSConstants.END_OF_CHAIN) {
int thisBlock = nextBlock;
loopDetector.claim(thisBlock);
nextBlock = blockStore.getNextBlock(thisBlock);
+ clearBlock(thisBlock);
blockStore.setNextBlock(thisBlock, POIFSConstants.UNUSED_BLOCK);
}
this.startBlock = POIFSConstants.END_OF_CHAIN;
}
+ /**
+ * Zeros a block which is being freed. Free blocks are written out
+ * with the file system, so they would otherwise keep the data of
+ * deleted or replaced streams.
+ */
+ private void clearBlock(int block) throws IOException {
+ final ByteBuffer buffer;
+ try {
+ buffer = blockStore.getBlockAt(block);
+ } catch (IndexOutOfBoundsException | NoSuchElementException e) {
+ // the block isn't backed by any data (e.g. a truncated file), so
there is nothing to clear
+ return;
+ }
+ while (buffer.hasRemaining()) {
+ buffer.put((byte)0);
+ }
+ blockStore.releaseBuffer(buffer);
+ }
+
/**
* Class that handles a streaming read of one stream
*/
diff --git
a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java
b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java
index 756df2f78e..646e236cb1 100644
--- a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java
+++ b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java
@@ -37,6 +37,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
+import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@@ -2600,6 +2601,79 @@ final class TestPOIFSStream {
return count;
}
+ /**
+ * Free blocks are written out with the file system, so they mustn't keep
+ * the data of deleted or replaced streams
+ */
+ @Test
+ void deletedDataIsNotWrittenOut() throws IOException {
+ byte[] data = new byte[10_000];
+ for (int i = 0; i < data.length; i++) {
+ data[i] = (byte)('A' + i % 26);
+ }
+ byte[] marker = Arrays.copyOf(data, 26);
+
+ byte[] original;
+ try (POIFSFileSystem fs = new POIFSFileSystem()) {
+ // big block streams and one in the mini stream
+ fs.createDocument(new ByteArrayInputStream(data), "Big");
+ fs.createDocument(new ByteArrayInputStream(data), "Replaced");
+ fs.createDocument(new ByteArrayInputStream(data, 0, 1_000),
"Small");
+ original = writeOut(fs);
+ }
+ assertTrue(contains(original, marker));
+
+ byte[] modified;
+ try (POIFSFileSystem fs = new POIFSFileSystem(new
ByteArrayInputStream(original))) {
+ deleteAndReplace(fs);
+ modified = writeOut(fs);
+ }
+ assertFalse(contains(modified, marker));
+ try (POIFSFileSystem fs = new POIFSFileSystem(new
ByteArrayInputStream(modified))) {
+ assertEquals(1, fs.getRoot().getEntryCount());
+ assertEquals(10,
((DocumentEntry)fs.getRoot().getEntry("Replaced")).getSize());
+ }
+
+ // same when the file is updated in place
+ File file = TempFile.createTempFile("poifs-free-blocks", ".ole2");
+ try {
+ Files.write(file.toPath(), original);
+ try (POIFSFileSystem fs = new POIFSFileSystem(file, false)) {
+ deleteAndReplace(fs);
+ fs.writeFilesystem();
+ }
+ modified = Files.readAllBytes(file.toPath());
+ assertFalse(contains(modified, marker));
+ } finally {
+ assertTrue(file.delete());
+ }
+ }
+
+ private static void deleteAndReplace(POIFSFileSystem fs) throws
IOException {
+ assertTrue(fs.getRoot().getEntry("Big").delete());
+ assertTrue(fs.getRoot().getEntry("Small").delete());
+ fs.createOrUpdateDocument(new ByteArrayInputStream(new byte[10]),
"Replaced");
+ }
+
+ private static byte[] writeOut(POIFSFileSystem fs) throws IOException {
+ UnsynchronizedByteArrayOutputStream bos =
UnsynchronizedByteArrayOutputStream.builder().get();
+ fs.writeFilesystem(bos);
+ return bos.toByteArray();
+ }
+
+ private static boolean contains(byte[] data, byte[] part) {
+ outer:
+ for (int i = 0; i + part.length <= data.length; i++) {
+ for (int j = 0; j < part.length; j++) {
+ if (data[i + j] != part[j]) {
+ continue outer;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
/**
* To ensure we can create a file >2gb in size, as well as to
* extend existing files past the 2gb boundary.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]