Skip to content

Commit 3bf6785

Browse files
authored
Merge pull request from GHSA-qcwq-55hx-v3vh
* asserted chunksize should be in the bounds of 0-java.outofmmeoryexception * asserted chunksize should be in the bounds of 0-java.outofmmeoryexception * https://github.com/xerial/snappy-java-ghsa-qcwq-55hx-v3vh/pull/2 * advisory-fix-3 * added and changed method name for happy and sad cases in SnappyTest.java * removed expected error for happy case in unit testing * added another unit test case in SnappyTest.java and fixed comments in SnappyInputStream.java * switched SnappyError to INVALID_CHUNK_SIZE * Updated unit tests * Resolved conflicts with another PR merge
1 parent 820e2e0 commit 3bf6785

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/java/org/xerial/snappy/SnappyInputStream.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,20 @@ protected boolean hasNextChunk()
417417
}
418418
}
419419

420+
// chunkSize is negative
421+
if (chunkSize < 0) {
422+
throw new SnappyError(SnappyErrorCode.INVALID_CHUNK_SIZE, "chunkSize is too big or negative : " + chunkSize);
423+
}
424+
420425
// extend the compressed data buffer size
421426
if (compressed == null || chunkSize > compressed.length) {
422-
compressed = new byte[chunkSize];
427+
// chunkSize exceeds limit
428+
try {
429+
compressed = new byte[chunkSize];
430+
}
431+
catch (java.lang.OutOfMemoryError e) {
432+
throw new SnappyError(SnappyErrorCode.INVALID_CHUNK_SIZE, e.getMessage());
433+
}
423434
}
424435
readBytes = 0;
425436
while (readBytes < chunkSize) {

src/test/java/org/xerial/snappy/SnappyTest.java

+50-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import static org.junit.Assert.*;
2828

29+
import java.io.ByteArrayInputStream;
2930
import java.io.IOException;
3031
import java.nio.ByteBuffer;
3132

@@ -330,14 +331,61 @@ public void isValidCompressedData()
330331
}
331332
}
332333

334+
/*
335+
336+
Tests happy cases for SnappyInputStream.read method
337+
- {0}
338+
*/
339+
@Test
340+
public void isValidChunkLengthForSnappyInputStreamIn()
341+
throws Exception {
342+
byte[] data = {0};
343+
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(data));
344+
byte[] out = new byte[50];
345+
in.read(out);
346+
}
347+
348+
/*
349+
Tests sad cases for SnappyInputStream.read method
350+
- Expects a java.lang.NegativeArraySizeException catched into a SnappyError
351+
- {-126, 'S', 'N', 'A', 'P', 'P', 'Y', 0, 0, 0, 0, 0, 0, 0, 0, 0,(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff}
352+
*/
353+
@Test(expected = SnappyError.class)
354+
public void isInvalidChunkLengthForSnappyInputStreamInNegative()
355+
throws Exception {
356+
byte[] data = {-126, 'S', 'N', 'A', 'P', 'P', 'Y', 0, 0, 0, 0, 0, 0, 0, 0, 0,(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff};
357+
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(data));
358+
byte[] out = new byte[50];
359+
in.read(out);
360+
}
361+
362+
/*
363+
Tests sad cases for SnappyInputStream.read method
364+
- Expects a java.lang.OutOfMemoryError
365+
- {-126, 'S', 'N', 'A', 'P', 'P', 'Y', 0, 0, 0, 0, 0, 0, 0, 0, 0,(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff}
366+
*/
367+
@Test(expected = SnappyError.class)
368+
public void isInvalidChunkLengthForSnappyInputStreamOutOfMemory()
369+
throws Exception {
370+
byte[] data = {-126, 'S', 'N', 'A', 'P', 'P', 'Y', 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff};
371+
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(data));
372+
byte[] out = new byte[50];
373+
try {
374+
in.read(out);
375+
} catch (Exception ignored) {
376+
// Exception here will be catched
377+
// But OutOfMemoryError will not be caught, and will still be thrown
378+
}
379+
}
380+
333381
/*
334382
Tests happy cases for BitShuffle.shuffle method
335383
- double: 0, 10
336384
- float: 0, 10
337385
- int: 0, 10
338386
- long: 0, 10
339387
- short: 0, 10
340-
*/
388+
*/
341389
@Test
342390
public void isValidArrayInputLengthForBitShuffleShuffle()
343391
throws Exception
@@ -386,5 +434,6 @@ public void isTooLargeLongArrayInputLengthForBitShuffleShuffle() throws Exceptio
386434
@Test(expected = SnappyError.class)
387435
public void isTooLargeShortArrayInputLengthForBitShuffleShuffle() throws Exception {
388436
BitShuffle.shuffle(new short[Integer.MAX_VALUE / 2 + 1]);
437+
389438
}
390439
}

0 commit comments

Comments
 (0)