Skip to content

Commit beff689

Browse files
committed
Reject non-12-byte inputs in BINARY_AS_INT96_TIMESTAMP_COMPARATOR
Without the guard, a malformed Binary throws IndexOutOfBoundsException from inside the stats-pruning path (for length < 12) or silently mis-compares by reading only the first 12 bytes (for length > 12). Throw IllegalArgumentException with a clear message instead, matching the precedent set by Binary.get2BytesLittleEndian for FLOAT16.
1 parent dc2197f commit beff689

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

parquet-column/src/main/java/org/apache/parquet/schema/PrimitiveComparator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public String toString() {
215215
static final PrimitiveComparator<Binary> BINARY_AS_INT96_TIMESTAMP_COMPARATOR = new BinaryComparator() {
216216
@Override
217217
int compareBinary(Binary b1, Binary b2) {
218+
if (b1.length() != 12 || b2.length() != 12) {
219+
throw new IllegalArgumentException(
220+
"INT96 binary length must be 12, got " + b1.length() + " and " + b2.length());
221+
}
218222
ByteBuffer bb1 = b1.toByteBuffer().slice();
219223
ByteBuffer bb2 = b2.toByteBuffer().slice();
220224
bb1.order(java.nio.ByteOrder.LITTLE_ENDIAN);

parquet-column/src/test/java/org/apache/parquet/schema/TestPrimitiveComparator.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,27 @@ public void testInt96Comparator() {
355355
}
356356
}
357357

358+
@Test
359+
public void testInt96ComparatorRejectsInvalidLengths() {
360+
Binary valid = timestampToInt96("2020-01-01T00:00:00.000");
361+
int[] invalidLengths = {0, 4, 8, 11, 13, 16};
362+
for (int len : invalidLengths) {
363+
Binary wrongLength = Binary.fromConstantByteArray(new byte[len]);
364+
try {
365+
BINARY_AS_INT96_TIMESTAMP_COMPARATOR.compare(wrongLength, valid);
366+
fail("Expected IllegalArgumentException when left operand has length " + len);
367+
} catch (IllegalArgumentException expected) {
368+
// ok
369+
}
370+
try {
371+
BINARY_AS_INT96_TIMESTAMP_COMPARATOR.compare(valid, wrongLength);
372+
fail("Expected IllegalArgumentException when right operand has length " + len);
373+
} catch (IllegalArgumentException expected) {
374+
// ok
375+
}
376+
}
377+
}
378+
358379
@Test
359380
public void testFloat16Comparator() {
360381
Binary[] valuesInAscendingOrder = {

0 commit comments

Comments
 (0)