Skip to content

Commit a724a2f

Browse files
committed
Fix offset 0 handling by LZ4 decompression
1 parent 6cceaea commit a724a2f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ public static int decompress(DataInput compressed, int decompressedLen, byte[] d
111111

112112
// matchs
113113
final int matchDec = compressed.readShort() & 0xFFFF;
114-
assert matchDec > 0;
114+
if (matchDec == 0) {
115+
throw new IOException("offset 0 is invalid");
116+
}
115117

116118
int matchLen = token & 0x0F;
117119
if (matchLen == 0x0F) {

lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,18 @@ private void doTest(byte[] data, int offset, int length, LZ4.HashTable hashTable
143143

144144
// Now restore and compare bytes
145145
byte[] restored = new byte[length + random().nextInt(10)];
146-
LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, 0);
146+
int restoreOffsetEnd = LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, 0);
147+
assertEquals(length, restoreOffsetEnd);
147148
assertArrayEquals(
148149
ArrayUtil.copyOfSubArray(data, offset, offset + length),
149150
ArrayUtil.copyOfSubArray(restored, 0, length));
150151

151152
// Now restore with an offset
152153
int restoreOffset = TestUtil.nextInt(random(), 1, 10);
153154
restored = new byte[restoreOffset + length + random().nextInt(10)];
154-
LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, restoreOffset);
155+
restoreOffsetEnd =
156+
LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, restoreOffset);
157+
assertEquals(length, restoreOffsetEnd - restoreOffset);
155158
assertArrayEquals(
156159
ArrayUtil.copyOfSubArray(data, offset, offset + length),
157160
ArrayUtil.copyOfSubArray(restored, restoreOffset, restoreOffset + length));
@@ -381,4 +384,34 @@ public void testUseDictionary() throws IOException {
381384
// own
382385
assertTrue(out.size() < len);
383386
}
387+
388+
public void testDecompressOffset0() {
389+
byte[] input =
390+
new byte[] {
391+
// token
392+
0xE,
393+
// offset 0
394+
0,
395+
0,
396+
// last literal
397+
// token
398+
7 << 4,
399+
// literal
400+
0,
401+
0,
402+
0,
403+
0,
404+
0,
405+
0,
406+
0
407+
};
408+
409+
byte[] output = new byte[18];
410+
411+
var e =
412+
assertThrows(
413+
IOException.class,
414+
() -> LZ4.decompress(new ByteArrayDataInput(input), output.length, output, 0));
415+
assertEquals("offset 0 is invalid", e.getMessage());
416+
}
384417
}

0 commit comments

Comments
 (0)