diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java index aa0ec49b57..c5e8c0f89c 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java @@ -49,7 +49,7 @@ public void initFromPage(int valueCount, ByteBufferInputStream stream) throws IO @Override public Binary readBytes() { - int length = lengthReader.readInteger(); + int length = readLength(); try { return Binary.fromConstantByteBuffer(in.slice(length)); } catch (IOException e) { @@ -64,9 +64,9 @@ public void skip() { @Override public void skip(int n) { - int length = 0; + long length = 0; for (int i = 0; i < n; ++i) { - length += lengthReader.readInteger(); + length += readLength(); } try { in.skipFully(length); @@ -74,4 +74,12 @@ public void skip(int n) { throw new ParquetDecodingException("Failed to skip " + length + " bytes"); } } + + private int readLength() { + int length = lengthReader.readInteger(); + if (length < 0) { + throw new ParquetDecodingException("Invalid DELTA_LENGTH_BYTE_ARRAY value length: " + length); + } + return length; + } } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java b/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java index 24689e7bcb..eb52aff2d2 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java @@ -19,12 +19,19 @@ package org.apache.parquet.column.values.deltalengthbytearray; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.Utils; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader; +import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesWriterForInteger; +import org.apache.parquet.io.ParquetDecodingException; import org.apache.parquet.io.api.Binary; import org.junit.jupiter.api.Test; @@ -99,4 +106,49 @@ public void testLengths() throws IOException { assertThat(bin[i]).isEqualTo(values[i].length()); } } + + @Test + public void testNegativeLength() throws IOException { + DeltaLengthByteArrayValuesReader reader = readerFor("abcd", 2, -2, 2); + + assertThat(reader.readBytes()).isEqualTo(Binary.fromString("ab")); + assertThatThrownBy(reader::readBytes) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid DELTA_LENGTH_BYTE_ARRAY value length: -2"); + } + + @Test + public void testSkipNegativeLength() throws IOException { + DeltaLengthByteArrayValuesReader reader = readerFor("abcd", 2, -2, 2); + + assertThatThrownBy(() -> reader.skip(3)) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid DELTA_LENGTH_BYTE_ARRAY value length: -2"); + } + + @Test + public void testSkipLengthsOverflowingInt() throws IOException { + DeltaLengthByteArrayValuesReader reader = readerFor("abcd", Integer.MAX_VALUE, Integer.MAX_VALUE); + + assertThatThrownBy(() -> reader.skip(2)) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Failed to skip 4294967294 bytes"); + } + + private static DeltaLengthByteArrayValuesReader readerFor(String data, int... lengths) throws IOException { + DeltaLengthByteArrayValuesReader reader = new DeltaLengthByteArrayValuesReader(); + reader.initFromPage(lengths.length, pageWithLengths(data, lengths)); + return reader; + } + + private static ByteBufferInputStream pageWithLengths(String data, int... lengths) throws IOException { + DeltaBinaryPackingValuesWriterForInteger lengthWriter = + new DeltaBinaryPackingValuesWriterForInteger(64, 64, new DirectByteBufferAllocator()); + for (int length : lengths) { + lengthWriter.writeInteger(length); + } + byte[] page = BytesInput.concat(lengthWriter.getBytes(), BytesInput.from(data.getBytes(StandardCharsets.UTF_8))) + .toByteArray(); + return ByteBufferInputStream.wrap(ByteBuffer.wrap(page)); + } } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java b/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java index 7078eb7793..bace90a95a 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java @@ -19,14 +19,19 @@ package org.apache.parquet.column.values.deltastrings; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; +import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.Utils; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader; +import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesWriterForInteger; +import org.apache.parquet.io.ParquetDecodingException; import org.apache.parquet.io.api.Binary; import org.junit.jupiter.api.Test; @@ -150,4 +155,28 @@ public void testReusedBackingArrayRegression() throws Exception { assertThat(decoded[1]).isEqualTo(Binary.fromString("parquet-111")); assertThat(decoded[2]).isEqualTo(Binary.fromString("parquet-222")); } + + @Test + public void testNegativeSuffixLength() throws IOException { + DeltaBinaryPackingValuesWriterForInteger prefixLengths = + new DeltaBinaryPackingValuesWriterForInteger(64, 64, new DirectByteBufferAllocator()); + DeltaBinaryPackingValuesWriterForInteger suffixLengths = + new DeltaBinaryPackingValuesWriterForInteger(64, 64, new DirectByteBufferAllocator()); + for (int length : new int[] {2, -2, 2}) { + prefixLengths.writeInteger(0); + suffixLengths.writeInteger(length); + } + byte[] page = BytesInput.concat( + prefixLengths.getBytes(), + suffixLengths.getBytes(), + BytesInput.from("abcd".getBytes(StandardCharsets.UTF_8))) + .toByteArray(); + DeltaByteArrayReader reader = new DeltaByteArrayReader(); + reader.initFromPage(3, ByteBufferInputStream.wrap(ByteBuffer.wrap(page))); + + assertThat(reader.readBytes()).isEqualTo(Binary.fromString("ab")); + assertThatThrownBy(reader::readBytes) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid DELTA_LENGTH_BYTE_ARRAY value length: -2"); + } }