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 a400d35eec RLEDecompressingInputStream: recover from bad chunks more 
consistently (#1497)
a400d35eec is described below

commit a400d35eecdbe22b3cab4b6698fa88bc9a8b53b9
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Oct 28 19:38:25 2026 +0000

    RLEDecompressingInputStream: recover from bad chunks more consistently 
(#1497)
    
    * RLEDecompressingInputStream: recover from bad chunks more consistently
    
    Follow-up to #1496.
    
    - VBAMacroReader: a module whose stream was read before its dir stream now
      falls back to the brute-force search when the dir offset is bad, as the
      other read order already did. A dir stream that fails to decompress is
      reported as an IOException with the record id, like other dir errors.
    - A chunk that fails to decode leaves the stream at its end instead of
      leaving bytes from the previous chunk to be read.
    - Input that ends inside a literal or copy token keeps the bytes already
      decompressed in that chunk, as input that ends at a flag byte does.
    - Uncompressed chunks must hold exactly 4096 bytes, per MS-OVBA.
    - Tests: cover reading past an empty chunk, compare the 4096-byte chunk in
      one assertion.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * TestRLEDecompressingInputStream: drop forbidden InputStream.available() 
call
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * TestRLEDecompressingInputStream: check available() after a failed chunk 
under @SuppressForbidden
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/poi/poifs/macros/VBAMacroReader.java    | 23 ++++--
 .../poi/util/RLEDecompressingInputStream.java      | 14 +++-
 .../poi/poifs/macros/TestVBAMacroReader.java       | 82 ++++++++++++++++++++++
 .../poi/util/TestRLEDecompressingInputStream.java  | 75 +++++++++++++++++---
 4 files changed, 175 insertions(+), 19 deletions(-)

diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java 
b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java
index dcc3a58f26..616626afc2 100644
--- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java
+++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java
@@ -268,15 +268,24 @@ public class VBAMacroReader implements Closeable {
             // Would adding module.read(in) here be correct?
         } else {
             // Decompress a previously found module and store the decompressed 
result into module.buf
-            InputStream stream = new RLEDecompressingInputStream(
+            final byte[] compressed = module.buf;
+            try (InputStream stream = new RLEDecompressingInputStream(
                     UnsynchronizedByteArrayInputStream.builder().
-                            setByteArray(module.buf).
+                            setByteArray(compressed).
                             setOffset(moduleOffset).
-                            setLength(module.buf.length - moduleOffset).
+                            setLength(compressed.length - moduleOffset).
                             get()
-            );
-            module.read(stream);
-            stream.close();
+            )) {
+                module.read(stream);
+            } catch (IllegalArgumentException | IllegalStateException e) {
+                //bad moduleOffset, try brute force as 
readModuleFromDocumentStream does
+                byte[] decompressedBytes = findCompressedStreamWBruteForce(
+                        
UnsynchronizedByteArrayInputStream.builder().setByteArray(compressed).get());
+                if (decompressedBytes == null) {
+                    throw e;
+                }
+                
module.read(UnsynchronizedByteArrayInputStream.builder().setByteArray(decompressedBytes).get());
+            }
         }
     }
 
@@ -623,7 +632,7 @@ public class VBAMacroReader implements Closeable {
                             break;
                     }
                 }
-            } catch (final IOException e) {
+            } catch (final IOException | IllegalArgumentException | 
IllegalStateException e) {
                 throw new IOException(
                         "Error occurred while reading macros at section id "
                                 + recordId + " (" + 
HexDump.shortToHex(recordId) + ")", e);
diff --git 
a/poi/src/main/java/org/apache/poi/util/RLEDecompressingInputStream.java 
b/poi/src/main/java/org/apache/poi/util/RLEDecompressingInputStream.java
index 48b1f83da4..697c7e536e 100644
--- a/poi/src/main/java/org/apache/poi/util/RLEDecompressingInputStream.java
+++ b/poi/src/main/java/org/apache/poi/util/RLEDecompressingInputStream.java
@@ -108,7 +108,7 @@ public class RLEDecompressingInputStream extends 
InputStream {
         int length = l;
         while (length > 0) {
             if (pos >= len) {
-                if ((len = readChunk()) == -1) {
+                if (len == -1 || (len = readChunk()) == -1) {
                     return offset > off ? offset - off : -1;
                 }
             }
@@ -155,6 +155,8 @@ public class RLEDecompressingInputStream extends 
InputStream {
      */
     private int readChunk() throws IOException {
         pos = 0;
+        // a chunk that fails to decode leaves the stream at its end, not with 
stale bytes in buf
+        len = -1;
         int w = readShort(in);
         if (w == -1 || w == 0) {
             return -1;
@@ -165,6 +167,11 @@ public class RLEDecompressingInputStream extends 
InputStream {
         }
         boolean rawChunk = (w & 0x8000) == 0;
         if (rawChunk) {
+            // MS-OVBA: an uncompressed chunk always holds exactly 4096 bytes
+            if (chunkSize != buf.length) {
+                throw new IllegalStateException(String.format(Locale.ROOT,
+                    "Uncompressed chunk must hold %d bytes, header gives %d", 
buf.length, chunkSize));
+            }
             if (IOUtils.readFully(in, buf, 0, chunkSize) < chunkSize) {
                 throw new IllegalStateException(String.format(Locale.ROOT, 
"Not enough bytes read, expected %d", chunkSize));
             }
@@ -186,7 +193,8 @@ public class RLEDecompressingInputStream extends 
InputStream {
                         // literal
                         final int b = in.read();
                         if (b == -1) {
-                            return -1;
+                            // truncated input: keep what was decompressed so 
far, as for a missing flag byte
+                            return outOffset;
                         }
                         if (outOffset >= buf.length) {
                             throw new 
IllegalStateException(String.format(Locale.ROOT,
@@ -198,7 +206,7 @@ public class RLEDecompressingInputStream extends 
InputStream {
                         // compressed token
                         int token = readShort(in);
                         if (token == -1) {
-                            return -1;
+                            return outOffset;
                         }
                         inOffset += 2;
                         int copyLenBits = getCopyLenBits(outOffset - 1);
diff --git 
a/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java 
b/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java
index 1f2cdd7574..26a5c67fe1 100644
--- a/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java
+++ b/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java
@@ -18,6 +18,7 @@
 package org.apache.poi.poifs.macros;
 
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.StringUtil;
@@ -463,6 +464,87 @@ class TestVBAMacroReader {
         return bos.toByteArray();
     }
 
+    /**
+     * The module stream can be read before the dir stream that holds its 
offset (bug 59858 layout).
+     * A bad offset must then fall back to the brute-force search, as it does 
in the other order.
+     */
+    @Test
+    void badModuleOffsetFromLaterDirStream() throws IOException {
+        final String source = "Attribute VB_Name = \"Module1\"";
+        // the module stream starts with a chunk whose first token is a copy 
token,
+        // the real compressed source follows it
+        final ByteArrayOutputStream moduleStream = new ByteArrayOutputStream();
+        moduleStream.write(new byte[]{0x01, 0x02, (byte) 0xB0, 0x01, 0x00, 
0x00});
+        
moduleStream.write(compressLiterals(source.getBytes(StandardCharsets.US_ASCII)));
+
+        final ByteArrayOutputStream dir = new ByteArrayOutputStream();
+        // MODULE_STREAM_NAME "Module1", then MODULE_OFFSET 0, which points at 
the broken chunk
+        writeShort(dir, 0x1A);
+        writeInt(dir, 7);
+        dir.write("Module1".getBytes(StandardCharsets.US_ASCII));
+        writeShort(dir, 0x32);
+        writeInt(dir, 14);
+        dir.write("Module1".getBytes(StandardCharsets.UTF_16LE));
+        writeShort(dir, 0x31);
+        writeInt(dir, 4);
+        writeInt(dir, 0);
+
+        try (POIFSFileSystem fs = new POIFSFileSystem();
+             VBAMacroReader reader = new VBAMacroReader(fs)) {
+            DirectoryNode moduleDir = (DirectoryNode) 
fs.getRoot().createDirectory("first").createDirectory("VBA");
+            moduleDir.createDocument("Module1", new 
ByteArrayInputStream(moduleStream.toByteArray()));
+            DirectoryNode dirDir = (DirectoryNode) 
fs.getRoot().createDirectory("second").createDirectory("VBA");
+            dirDir.createDocument("dir", new 
ByteArrayInputStream(compressLiterals(dir.toByteArray())));
+
+            VBAMacroReader.ModuleMap modules = new VBAMacroReader.ModuleMap();
+            reader.readMacros(moduleDir, modules);
+            reader.readMacros(dirDir, modules);
+            assertEquals(source, new String(modules.get("Module1").buf, 
StandardCharsets.US_ASCII));
+        }
+    }
+
+    /**
+     * a dir stream that fails to decompress is reported as an IOException
+     */
+    @Test
+    void badDirStreamIsIOException() throws IOException {
+        try (POIFSFileSystem fs = new POIFSFileSystem();
+             VBAMacroReader reader = new VBAMacroReader(fs)) {
+            DirectoryNode vba = (DirectoryNode) 
fs.getRoot().createDirectory("VBA");
+            // a chunk whose first token is a copy token
+            vba.createDocument("dir", new ByteArrayInputStream(
+                    new byte[]{0x01, 0x02, (byte) 0xB0, 0x01, 0x00, 0x00}));
+            VBAMacroReader.ModuleMap modules = new VBAMacroReader.ModuleMap();
+            assertThrows(IOException.class, () -> reader.readMacros(vba, 
modules));
+        }
+    }
+
+    /**
+     * RLE-compresses {@code data} into a single chunk of literals only
+     */
+    private static byte[] compressLiterals(byte[] data) {
+        final ByteArrayOutputStream chunk = new ByteArrayOutputStream();
+        for (int i = 0; i < data.length; i += 8) {
+            chunk.write(0);
+            chunk.write(data, i, Math.min(8, data.length - i));
+        }
+        final ByteArrayOutputStream out = new ByteArrayOutputStream();
+        out.write(0x01);
+        writeShort(out, 0xB000 | (chunk.size() - 1));
+        out.writeBytes(chunk.toByteArray());
+        return out.toByteArray();
+    }
+
+    private static void writeShort(ByteArrayOutputStream out, int value) {
+        out.write(value);
+        out.write(value >> 8);
+    }
+
+    private static void writeInt(ByteArrayOutputStream out, int value) {
+        writeShort(out, value);
+        writeShort(out, value >> 16);
+    }
+
     private static void putZipEntry(ZipOutputStream zos, String name, byte[] 
data) throws IOException {
         zos.putNextEntry(new ZipEntry(name));
         zos.write(data);
diff --git 
a/poi/src/test/java/org/apache/poi/util/TestRLEDecompressingInputStream.java 
b/poi/src/test/java/org/apache/poi/util/TestRLEDecompressingInputStream.java
index c107dadb7b..1933440c7f 100644
--- a/poi/src/test/java/org/apache/poi/util/TestRLEDecompressingInputStream.java
+++ b/poi/src/test/java/org/apache/poi/util/TestRLEDecompressingInputStream.java
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
 
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.junit.jupiter.api.Test;
@@ -210,11 +211,9 @@ class TestRLEDecompressingInputStream {
         final byte[] full = {
             0x01, 0x03, (byte)0xB0, 0x02, 0x41, (byte)0xFC, 0x0F
         };
-        byte[] expanded = RLEDecompressingInputStream.decompress(full);
-        assertEquals(4096, expanded.length);
-        for (byte b : expanded) {
-            assertEquals(0x41, b);
-        }
+        final byte[] expected = new byte[4096];
+        Arrays.fill(expected, (byte) 0x41);
+        assertArrayEquals(expected, 
RLEDecompressingInputStream.decompress(full));
 
         // the same chunk followed by the literal 'B'
         final byte[] overflow = {
@@ -224,25 +223,83 @@ class TestRLEDecompressingInputStream {
     }
 
     /**
-     * a chunk without any token must not repeat a byte of the previous chunk
+     * a chunk without any token must not repeat a byte of the previous chunk,
+     * and reading must carry on with the chunk after it
      */
     @Test
     void emptyChunkDoesNotRepeatBytes() throws IOException {
-        // "abc", then a compressed chunk consisting of a flag byte only, then 
a chunk header without flag byte
+        // "abc", then a compressed chunk consisting of a flag byte only, then 
"d",
+        // then a chunk header without flag byte
         final byte[] compressed = {
-            0x01, 0x03, (byte)0xB0, 0x00, 0x61, 0x62, 0x63, 0x00, (byte)0xB0, 
0x00, 0x05, (byte)0xB0
+            0x01, 0x03, (byte)0xB0, 0x00, 0x61, 0x62, 0x63, 0x00, (byte)0xB0, 
0x00,
+            0x01, (byte)0xB0, 0x00, 0x64, 0x05, (byte)0xB0
         };
-        checkRLEDecompression("abc", compressed);
+        checkRLEDecompression("abcd", compressed);
 
         try (InputStream stream = new RLEDecompressingInputStream(new 
ByteArrayInputStream(compressed))) {
             assertEquals('a', stream.read());
             assertEquals('b', stream.read());
             assertEquals('c', stream.read());
+            assertEquals('d', stream.read());
+            assertEquals(-1, stream.read());
             assertEquals(-1, stream.read());
+        }
+    }
+
+    /**
+     * a chunk that fails to decode must not leave bytes of the previous chunk 
to be read
+     */
+    @Test
+    @SuppressForbidden("checks available() reports nothing after a failed 
chunk")
+    void failedChunkLeavesNoStaleBytes() throws IOException {
+        // "abcd", then a chunk with the literal 'x' and a copy token reaching 
2 bytes back
+        final byte[] compressed = {
+            0x01, 0x04, (byte)0xB0, 0x00, 0x61, 0x62, 0x63, 0x64,
+            0x03, (byte)0xB0, 0x02, 0x78, 0x00, 0x10
+        };
+        try (InputStream stream = new RLEDecompressingInputStream(new 
ByteArrayInputStream(compressed))) {
+            final byte[] b = new byte[4];
+            assertEquals(4, stream.read(b));
+            assertArrayEquals("abcd".getBytes(StringUtil.UTF8), b);
+            assertThrows(IllegalStateException.class, stream::read);
             assertEquals(-1, stream.read());
+            assertEquals(-1, stream.read(b));
+            assertEquals(0, stream.skip(1));
+            assertEquals(0, stream.available());
         }
     }
 
+    /**
+     * input that ends inside a literal or a copy token keeps the bytes 
decompressed before it,
+     * as input that ends at a flag byte does
+     */
+    @Test
+    void truncatedChunkKeepsDecompressedBytes() throws IOException {
+        // a flag byte for 8 literals, but the input ends after "ab"
+        final byte[] midLiteral = {
+            0x01, 0x05, (byte)0xB0, 0x00, 0x61, 0x62
+        };
+        checkRLEDecompression("ab", midLiteral);
+
+        // "ab", then a flag byte for a copy token, but only one byte of it 
follows
+        final byte[] midCopyToken = {
+            0x01, 0x05, (byte)0xB0, 0x04, 0x61, 0x62, 0x00
+        };
+        checkRLEDecompression("ab", midCopyToken);
+    }
+
+    /**
+     * an uncompressed chunk always holds 4096 bytes
+     */
+    @Test
+    void shortUncompressedChunk() {
+        // raw chunk (bit 15 unset) with size field 0x002: 3 bytes
+        final byte[] compressed = {
+            0x01, 0x02, 0x30, 0x61, 0x62, 0x63
+        };
+        assertThrows(IllegalStateException.class, () -> 
RLEDecompressingInputStream.decompress(compressed));
+    }
+
     private static void checkRLEDecompression(String expected, byte[] 
runLengthEncodedData) throws IOException {
         InputStream compressedStream = new 
ByteArrayInputStream(runLengthEncodedData);
         UnsynchronizedByteArrayOutputStream out = 
UnsynchronizedByteArrayOutputStream.builder().get();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to