@@ -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