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 07f1631dd6 RLEDecompressingInputStream: bound copy tokens to the chunk
buffer (#1496)
07f1631dd6 is described below
commit 07f1631dd66f647aacf2f43e3f408fe033aac4d0
Author: Nayyar <[email protected]>
AuthorDate: Tue Oct 27 23:37:23 2026 +0530
RLEDecompressingInputStream: bound copy tokens to the chunk buffer (#1496)
---
.../poi/util/RLEDecompressingInputStream.java | 18 +++++-
.../poi/util/TestRLEDecompressingInputStream.java | 67 ++++++++++++++++++++++
2 files changed, 84 insertions(+), 1 deletion(-)
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 d672381e58..48b1f83da4 100644
--- a/poi/src/main/java/org/apache/poi/util/RLEDecompressingInputStream.java
+++ b/poi/src/main/java/org/apache/poi/util/RLEDecompressingInputStream.java
@@ -87,7 +87,7 @@ public class RLEDecompressingInputStream extends InputStream {
if (len == -1) {
return -1;
}
- if (pos >= len) {
+ while (pos >= len) {
if ((len = readChunk()) == -1) {
return -1;
}
@@ -188,6 +188,10 @@ public class RLEDecompressingInputStream extends
InputStream {
if (b == -1) {
return -1;
}
+ if (outOffset >= buf.length) {
+ throw new
IllegalStateException(String.format(Locale.ROOT,
+ "Decompressed chunk exceeds the maximum of %d
bytes", buf.length));
+ }
buf[outOffset++] = (byte) b;
inOffset++;
} else {
@@ -200,6 +204,18 @@ public class RLEDecompressingInputStream extends
InputStream {
int copyLenBits = getCopyLenBits(outOffset - 1);
int copyOffset = (token >> (copyLenBits)) + 1;
int copyLen = (token & (POWER2[copyLenBits] - 1)) + 3;
+ // MS-OVBA 2.4.1.3.19: a CopyToken may only reference
bytes already
+ // decompressed in the current chunk, and a chunk
never exceeds 4096 bytes
+ if (copyOffset > outOffset) {
+ throw new
IllegalStateException(String.format(Locale.ROOT,
+ "Copy token offset %d reaches before the start
of the chunk (%d bytes decompressed)",
+ copyOffset, outOffset));
+ }
+ if (copyLen > buf.length - outOffset) {
+ throw new
IllegalStateException(String.format(Locale.ROOT,
+ "Copy token length %d at offset %d exceeds the
maximum chunk size of %d bytes",
+ copyLen, outOffset, buf.length));
+ }
int startPos = outOffset - copyOffset;
int endPos = startPos + copyLen;
for (int i = startPos; i < endPos; i++) {
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 4d354d73df..c107dadb7b 100644
--- a/poi/src/test/java/org/apache/poi/util/TestRLEDecompressingInputStream.java
+++ b/poi/src/test/java/org/apache/poi/util/TestRLEDecompressingInputStream.java
@@ -19,6 +19,7 @@ package org.apache.poi.util;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -176,6 +177,72 @@ class TestRLEDecompressingInputStream {
}
}
+ /**
+ * a copy token as the first item of a chunk has nothing to copy from
+ */
+ @Test
+ void copyTokenBeforeChunkStart() {
+ // compressed chunk of 3 bytes: flag byte with item 0 set, copy token
0x0000 (offset 1, length 3)
+ final byte[] compressed = {
+ 0x01, 0x02, (byte)0xB0, 0x01, 0x00, 0x00
+ };
+ assertThrows(IllegalStateException.class, () ->
RLEDecompressingInputStream.decompress(compressed));
+ }
+
+ /**
+ * a copy token may not expand the chunk past 4096 bytes
+ */
+ @Test
+ void copyTokenPastChunkEnd() {
+ // literal 'A', then copy token 0x0FFF: offset 1, length 4098
+ final byte[] compressed = {
+ 0x01, 0x03, (byte)0xB0, 0x02, 0x41, (byte)0xFF, 0x0F
+ };
+ assertThrows(IllegalStateException.class, () ->
RLEDecompressingInputStream.decompress(compressed));
+ }
+
+ /**
+ * a chunk of exactly 4096 bytes is fine, one more literal is not
+ */
+ @Test
+ void literalPastChunkEnd() throws IOException {
+ // literal 'A', then copy token 0x0FFC: offset 1, length 4095 -> 4096
bytes
+ 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);
+ }
+
+ // the same chunk followed by the literal 'B'
+ final byte[] overflow = {
+ 0x01, 0x04, (byte)0xB0, 0x02, 0x41, (byte)0xFC, 0x0F, 0x42
+ };
+ assertThrows(IllegalStateException.class, () ->
RLEDecompressingInputStream.decompress(overflow));
+ }
+
+ /**
+ * a chunk without any token must not repeat a byte of the previous chunk
+ */
+ @Test
+ void emptyChunkDoesNotRepeatBytes() throws IOException {
+ // "abc", then a compressed chunk consisting of a flag byte only, 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
+ };
+ checkRLEDecompression("abc", compressed);
+
+ try (InputStream stream = new RLEDecompressingInputStream(new
ByteArrayInputStream(compressed))) {
+ assertEquals('a', stream.read());
+ assertEquals('b', stream.read());
+ assertEquals('c', stream.read());
+ assertEquals(-1, stream.read());
+ assertEquals(-1, stream.read());
+ }
+ }
+
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]