From 4805c8b840114a4132f15f6b5b23d7810e5e943e Mon Sep 17 00:00:00 2001 From: Arnab Nandy Date: Thu, 13 Aug 2026 18:19:23 +0530 Subject: [PATCH] GH-3715: Validate dictionary IDs while decoding Signed-off-by: Arnab Nandy --- .../dictionary/DictionaryValuesReader.java | 50 ++++++++----------- .../values/dictionary/TestDictionary.java | 16 ++++++ 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java index 8a98d29c77..b6116f337b 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java @@ -41,10 +41,13 @@ public class DictionaryValuesReader extends ValuesReader { private Dictionary dictionary; + private final int maxDictionaryId; + private RunLengthBitPackingHybridDecoder decoder; public DictionaryValuesReader(Dictionary dictionary) { this.dictionary = dictionary; + this.maxDictionaryId = dictionary.getMaxId(); } @Override @@ -80,62 +83,51 @@ public void skipInts(int n) throws IOException { @Override public int readValueDictionaryId() { - try { - return decoder.readInt(); - } catch (IOException e) { - throw new ParquetDecodingException(e); - } + return readDictionaryId(); } @Override public Binary readBytes() { - try { - return dictionary.decodeToBinary(decoder.readInt()); - } catch (IOException e) { - throw new ParquetDecodingException(e); - } + return dictionary.decodeToBinary(readDictionaryId()); } @Override public float readFloat() { - try { - return dictionary.decodeToFloat(decoder.readInt()); - } catch (IOException e) { - throw new ParquetDecodingException(e); - } + return dictionary.decodeToFloat(readDictionaryId()); } @Override public double readDouble() { - try { - return dictionary.decodeToDouble(decoder.readInt()); - } catch (IOException e) { - throw new ParquetDecodingException(e); - } + return dictionary.decodeToDouble(readDictionaryId()); } @Override public int readInteger() { - try { - return dictionary.decodeToInt(decoder.readInt()); - } catch (IOException e) { - throw new ParquetDecodingException(e); - } + return dictionary.decodeToInt(readDictionaryId()); } @Override public long readLong() { + return dictionary.decodeToLong(readDictionaryId()); + } + + @Override + public void skip() { try { - return dictionary.decodeToLong(decoder.readInt()); + decoder.readInt(); // Type does not matter as we are just skipping dictionary keys } catch (IOException e) { throw new ParquetDecodingException(e); } } - @Override - public void skip() { + private int readDictionaryId() { try { - decoder.readInt(); // Type does not matter as we are just skipping dictionary keys + int id = decoder.readInt(); + if (id < 0 || id > maxDictionaryId) { + throw new ParquetDecodingException( + "Dictionary id " + id + " is outside the valid range 0 to " + maxDictionaryId); + } + return id; } catch (IOException e) { throw new ParquetDecodingException(e); } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java b/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java index 44f90d901e..bd93c72864 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java @@ -827,6 +827,22 @@ public void testZeroValues() throws IOException { } } + @Test + public void testInvalidDictionaryId() throws IOException { + BytesInput dictionaryBytes = BytesInput.fromInt(34); + DictionaryPage dictionaryPage = new DictionaryPage(dictionaryBytes, 1, PLAIN); + ColumnDescriptor descriptor = new ColumnDescriptor(new String[] {"foo"}, INT32, 0, 0); + Dictionary dictionary = PLAIN.initDictionary(descriptor, dictionaryPage); + DictionaryValuesReader reader = new DictionaryValuesReader(dictionary); + + // A bit width of 1 can represent dictionary id 1, but this dictionary only contains id 0. + reader.initFromPage(1, BytesInput.from(new byte[] {0x01, 0x02, 0x01}).toInputStream()); + + assertThatThrownBy(reader::readInteger) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Dictionary id 1 is outside the valid range 0 to 0"); + } + @Test public void testBooleanDictionary() throws IOException { // Create a dictionary page with boolean values (false, true)