diff --git a/src/java/net/jpountz/lz4/LZ4DecompressorWithLength.java b/src/java/net/jpountz/lz4/LZ4DecompressorWithLength.java index c2cf409..5781474 100644 --- a/src/java/net/jpountz/lz4/LZ4DecompressorWithLength.java +++ b/src/java/net/jpountz/lz4/LZ4DecompressorWithLength.java @@ -348,7 +348,7 @@ public void decompress(ByteBuffer src, ByteBuffer dest) { * decompresses src[srcOff:] into dest[destOff:] * and returns the number of bytes read from src, and * when {@link LZ4SafeDecompressor} was specified to the constructor, - * decompresses src[srcOff:src.remaining()] into dest[destOff:] + * decompresses src[srcOff:src.limit()] into dest[destOff:] * and returns the number of decompressed bytes written into dest. * The positions and limits of the {@link ByteBuffer}s remain unchanged. * @@ -361,7 +361,7 @@ public void decompress(ByteBuffer src, ByteBuffer dest) { */ public int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff) { if (safeDecompressor != null) { - return decompress(src, srcOff, src.remaining() - srcOff, dest, destOff); + return decompress(src, srcOff, src.limit() - srcOff, dest, destOff); } final int destLen = getDecompressedLength(src, srcOff); ByteBufferUtils.checkRange(dest, destOff, 0); diff --git a/src/test/net/jpountz/lz4/OutOfBoundsTest.java b/src/test/net/jpountz/lz4/OutOfBoundsTest.java index 11be1db..35eb09c 100644 --- a/src/test/net/jpountz/lz4/OutOfBoundsTest.java +++ b/src/test/net/jpountz/lz4/OutOfBoundsTest.java @@ -177,6 +177,37 @@ public void destinationBufferLimitsDecompressedLength(LZ4DecompressorWithLength assertEquals(0, indexedDest.position()); } + @ParameterizedTest + @MethodSource("decompressorsWithLength") + public void byteBufferSourceOffsetIgnoresPosition(LZ4DecompressorWithLength decompressor) { + LZ4Factory factory = LZ4Factory.safeInstance(); + byte[] original = new byte[64]; + for (int i = 0; i < original.length; i++) { + original[i] = (byte) (i % 7); + } + byte[] compressed = new LZ4CompressorWithLength(factory.fastCompressor()).compress(original); + int srcOff = 3; + + for (boolean direct : new boolean[] {false, true}) { + ByteBuffer src = direct + ? ByteBuffer.allocateDirect(srcOff + compressed.length) + : ByteBuffer.allocate(srcOff + compressed.length); + src.position(srcOff); + src.put(compressed); + src.clear(); + for (int position : new int[] {0, 1, srcOff, src.limit()}) { + src.position(position); + ByteBuffer dest = direct ? ByteBuffer.allocateDirect(original.length) : ByteBuffer.allocate(original.length); + decompressor.decompress(src, srcOff, dest, 0); + assertEquals(position, src.position()); + assertEquals(0, dest.position()); + byte[] decompressed = new byte[original.length]; + dest.get(decompressed); + assertArrayEquals(original, decompressed); + } + } + } + @ParameterizedTest @MethodSource("limitedDecompressorsWithLength") public void configuredMaximumOnlyLimitsAllocatedOutput(LZ4DecompressorWithLength decompressor) {