Skip to content

Commit 820e2e0

Browse files
authored
Merge pull request from GHSA-pqr6-cmr2-h8hf
* Fixed integer overflow by checking if multiplication result is smaller than original value * Fixed integer overflow by checking if multiplication result is smaller than original value * Fixed integer overflow by checking if multiplication result is smaller than original value * imporved error messages and added happy and sad cases for unit test in SnappyTest.java * switched SnappyError into ILLEGAL_ARGUMENT in SnappyErrorCode.java and Snappy.java * wrote new and updated unit test methods * updated comments in SnappyTest.java * Fixed and updated unit tests in SnappyTest.java
1 parent 27e2ce0 commit 820e2e0

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public static int shuffle(ByteBuffer input, BitShuffleType type, ByteBuffer shuf
9191
* @throws IOException
9292
*/
9393
public static byte[] shuffle(short[] input) throws IOException {
94+
if (input.length * 2 < input.length) {
95+
throw new SnappyError(SnappyErrorCode.TOO_LARGE_INPUT, "input array size is too large: " + input.length);
96+
}
9497
byte[] output = new byte[input.length * 2];
9598
int numProcessed = impl.shuffle(input, 0, 2, input.length * 2, output, 0);
9699
assert(numProcessed == input.length * 2);
@@ -105,6 +108,9 @@ public static byte[] shuffle(short[] input) throws IOException {
105108
* @throws IOException
106109
*/
107110
public static byte[] shuffle(int[] input) throws IOException {
111+
if (input.length * 4 < input.length) {
112+
throw new SnappyError(SnappyErrorCode.TOO_LARGE_INPUT, "input array size is too large: " + input.length);
113+
}
108114
byte[] output = new byte[input.length * 4];
109115
int numProcessed = impl.shuffle(input, 0, 4, input.length * 4, output, 0);
110116
assert(numProcessed == input.length * 4);
@@ -119,6 +125,9 @@ public static byte[] shuffle(int[] input) throws IOException {
119125
* @throws IOException
120126
*/
121127
public static byte[] shuffle(long[] input) throws IOException {
128+
if (input.length * 8 < input.length) {
129+
throw new SnappyError(SnappyErrorCode.TOO_LARGE_INPUT, "input array size is too large: " + input.length);
130+
}
122131
byte[] output = new byte[input.length * 8];
123132
int numProcessed = impl.shuffle(input, 0, 8, input.length * 8, output, 0);
124133
assert(numProcessed == input.length * 8);
@@ -133,6 +142,9 @@ public static byte[] shuffle(long[] input) throws IOException {
133142
* @throws IOException
134143
*/
135144
public static byte[] shuffle(float[] input) throws IOException {
145+
if (input.length * 4 < input.length) {
146+
throw new SnappyError(SnappyErrorCode.TOO_LARGE_INPUT, "input array size is too large: " + input.length);
147+
}
136148
byte[] output = new byte[input.length * 4];
137149
int numProcessed = impl.shuffle(input, 0, 4, input.length * 4, output, 0);
138150
assert(numProcessed == input.length * 4);
@@ -147,6 +159,9 @@ public static byte[] shuffle(float[] input) throws IOException {
147159
* @throws IOException
148160
*/
149161
public static byte[] shuffle(double[] input) throws IOException {
162+
if (input.length * 8 < input.length) {
163+
throw new SnappyError(SnappyErrorCode.TOO_LARGE_INPUT, "input array size is too large: " + input.length);
164+
}
150165
byte[] output = new byte[input.length * 8];
151166
int numProcessed = impl.shuffle(input, 0, 8, input.length * 8, output, 0);
152167
assert(numProcessed == input.length * 8);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public enum SnappyErrorCode
4242
EMPTY_INPUT(6),
4343
INCOMPATIBLE_VERSION(7),
4444
INVALID_CHUNK_SIZE(8),
45-
UNSUPPORTED_PLATFORM(9);
45+
UNSUPPORTED_PLATFORM(9),
46+
TOO_LARGE_INPUT(10);
4647

4748
public final int id;
4849

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,62 @@ public void isValidCompressedData()
329329
_logger.debug(e);
330330
}
331331
}
332+
333+
/*
334+
Tests happy cases for BitShuffle.shuffle method
335+
- double: 0, 10
336+
- float: 0, 10
337+
- int: 0, 10
338+
- long: 0, 10
339+
- short: 0, 10
340+
*/
341+
@Test
342+
public void isValidArrayInputLengthForBitShuffleShuffle()
343+
throws Exception
344+
{
345+
byte[] b = BitShuffle.shuffle(new double[0]);
346+
byte[] c = BitShuffle.shuffle(new float[0]);
347+
byte[] d = BitShuffle.shuffle(new int[0]);
348+
byte[] e = BitShuffle.shuffle(new long[0]);
349+
byte[] f = BitShuffle.shuffle(new short[0]);
350+
byte[] n = BitShuffle.shuffle(new double[10]);
351+
byte[] o = BitShuffle.shuffle(new float[10]);
352+
byte[] p = BitShuffle.shuffle(new int[10]);
353+
byte[] q = BitShuffle.shuffle(new long[10]);
354+
byte[] r = BitShuffle.shuffle(new short[10]);
355+
}
356+
357+
/*
358+
Tests sad cases for BitShuffle.shuffle method
359+
- Allocate a buffer whose byte size will be a bit larger than Integer.MAX_VALUE
360+
- double: 8
361+
- float: 4
362+
- int: 4
363+
- long: 8
364+
- short: 2
365+
*/
366+
@Test(expected = SnappyError.class)
367+
public void isTooLargeDoubleArrayInputLengthForBitShuffleShuffle() throws Exception {
368+
BitShuffle.shuffle(new double[Integer.MAX_VALUE / 8 + 1]);
369+
}
370+
371+
@Test(expected = SnappyError.class)
372+
public void isTooLargeFloatArrayInputLengthForBitShuffleShuffle() throws Exception {
373+
BitShuffle.shuffle(new float[Integer.MAX_VALUE / 4 + 1]);
374+
}
375+
376+
@Test(expected = SnappyError.class)
377+
public void isTooLargeIntArrayInputLengthForBitShuffleShuffle() throws Exception {
378+
BitShuffle.shuffle(new float[Integer.MAX_VALUE / 4 + 1]);
379+
}
380+
381+
@Test(expected = SnappyError.class)
382+
public void isTooLargeLongArrayInputLengthForBitShuffleShuffle() throws Exception {
383+
BitShuffle.shuffle(new long[Integer.MAX_VALUE / 8 + 1]);
384+
}
385+
386+
@Test(expected = SnappyError.class)
387+
public void isTooLargeShortArrayInputLengthForBitShuffleShuffle() throws Exception {
388+
BitShuffle.shuffle(new short[Integer.MAX_VALUE / 2 + 1]);
389+
}
332390
}

0 commit comments

Comments
 (0)