From d2d88eabfe1ff55aac695f0f557fde380768c0b2 Mon Sep 17 00:00:00 2001 From: NopeCharon <115794254+NopeCharon@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:30:30 +0530 Subject: [PATCH] GH-2148: Reject zero-length runs in RunLengthBitPackingHybridDecoder A run header encoding zero values is not valid RLE/bit-packing hybrid data, but the decoder accepted it. For an RLE run, readInt() decremented the count from 0 to -1 and returned the run's value. The count never reached 0 again, so every later value on the page repeated that value and the rest of the stream was silently ignored. A bit-packed run with 0 groups read from an empty buffer and failed with an ArrayIndexOutOfBoundsException. skipInts() moved past such runs, so the same page could decode differently depending on whether values were read or skipped. Throw a ParquetDecodingException naming the invalid run when a header has a repeat count or group count of 0. This matches the Arrow C++ decoder, which also treats these headers as illegal. parquet-java's encoder never writes zero-length runs, so valid files are unaffected. Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com> --- .../rle/RunLengthBitPackingHybridDecoder.java | 6 ++ .../TestRunLengthBitPackingHybridDecoder.java | 68 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 parquet-column/src/test/java/org/apache/parquet/column/values/rle/TestRunLengthBitPackingHybridDecoder.java diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java index 2f143d2a07..e8456f4131 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java @@ -114,11 +114,17 @@ private void readNext() throws IOException { switch (mode) { case RLE: currentCount = header >>> 1; + if (currentCount == 0) { + throw new ParquetDecodingException("Invalid RLE run with a repeat count of 0"); + } LOG.debug("reading {} values RLE", currentCount); currentValue = BytesUtils.readIntLittleEndianPaddedOnBitWidth(in, bitWidth); break; case PACKED: int numGroups = header >>> 1; + if (numGroups == 0) { + throw new ParquetDecodingException("Invalid bit-packed run with 0 groups of 8 values"); + } currentCount = numGroups * 8; LOG.debug("reading {} values BIT PACKED", currentCount); if (currentBuffer.length < currentCount) { diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/rle/TestRunLengthBitPackingHybridDecoder.java b/parquet-column/src/test/java/org/apache/parquet/column/values/rle/TestRunLengthBitPackingHybridDecoder.java new file mode 100644 index 0000000000..82ea632d3e --- /dev/null +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/rle/TestRunLengthBitPackingHybridDecoder.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.parquet.column.values.rle; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.ByteBuffer; +import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.io.ParquetDecodingException; +import org.junit.jupiter.api.Test; + +public class TestRunLengthBitPackingHybridDecoder { + + @Test + public void testRleRunWithZeroRepeatCount() { + // An RLE run of 0 x 7 followed by a valid RLE run of 3 x 5 + RunLengthBitPackingHybridDecoder decoder = decoderFor(3, 0b0000_0000, 7, 0b0000_0110, 5); + + assertThatThrownBy(decoder::readInt) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid RLE run with a repeat count of 0"); + } + + @Test + public void testBitPackedRunWithZeroGroups() { + // A bit-packed run of 0 groups followed by a valid RLE run of 3 x 5 + RunLengthBitPackingHybridDecoder decoder = decoderFor(3, 0b0000_0001, 0b0000_0110, 5); + + assertThatThrownBy(decoder::readInt) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid bit-packed run with 0 groups of 8 values"); + } + + @Test + public void testSkipRleRunWithZeroRepeatCount() { + // Skipping must reject the empty run as well instead of silently moving on to the next one + RunLengthBitPackingHybridDecoder decoder = decoderFor(3, 0b0000_0000, 7, 0b0000_0110, 5); + + assertThatThrownBy(() -> decoder.skipInts(1)) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid RLE run with a repeat count of 0"); + } + + private static RunLengthBitPackingHybridDecoder decoderFor(int bitWidth, int... bytes) { + ByteBuffer buffer = ByteBuffer.allocate(bytes.length); + for (int b : bytes) { + buffer.put((byte) b); + } + buffer.flip(); + return new RunLengthBitPackingHybridDecoder(bitWidth, ByteBufferInputStream.wrap(buffer)); + } +}