Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/org/xerial/snappy/Snappy.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed)
int uPos = uncompressed.position();
int uLen = uncompressed.remaining();
int cPos = compressed.position();

int requiredCapacity = maxCompressedLength(uLen);
if (compressed.remaining() < requiredCapacity) {
throw new IllegalArgumentException("not enough space for output: need " + requiredCapacity
+ " bytes, but only " + compressed.remaining() + " remaining");
}

int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed,
cPos);

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/xerial/snappy/SnappyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,21 @@ public void isTooLargeShortArrayInputLengthForBitShuffleShuffle() throws Excepti
BitShuffle.shuffle(new short[Integer.MAX_VALUE / 2 + 1]);
}

@Test(expected = IllegalArgumentException.class)
public void testCompressWithInsufficientBuffer() throws Exception {
byte[] incompressible = new byte[1024 * 1024];
new java.util.Random(42).nextBytes(incompressible);

ByteBuffer src = ByteBuffer.allocateDirect(incompressible.length);
src.put(incompressible);
src.flip();

ByteBuffer dest = ByteBuffer.allocateDirect(64);

Snappy.compress(src, dest);
fail("Expected IllegalArgumentException but method completed normally");
}

private void assumingCIIsFalse() {
if (System.getenv("CI") == null)
return;
Expand Down
Loading