This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12324-8299de5ebc047674f230143edf719dfbbb29075b in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit c969269f507b070282b11696d447117b2e0282bf Author: Shashank Yadav <[email protected]> AuthorDate: Thu Nov 19 19:34:39 2026 +0000 fix(parquet): return error for invalid DELTA_LENGTH_BYTE_ARRAY lengths (#12324) # Which issue does this PR close? - Closes #12290 # Rationale for this change A Parquet page encoded with DELTA_LENGTH_BYTE_ARRAY (including the suffix stream of DELTA_BYTE_ARRAY) whose lengths are negative, or add up to more bytes than the page holds, makes the reader panic instead of returning an error. A negative length gives an out-of-range offset in `get`, and a length past the end panics in `Bytes::slice`. Malformed input should return a ParquetError. # What changes are included in this PR? - `DeltaLengthByteArrayDecoder::set_data` in `parquet/src/encodings/decoding.rs` now checks the decoded lengths when the page is set. A negative length, a sum that overflows, or a sum greater than the data after the length stream returns an error. - `DeltaByteArrayDecoder` uses the same decoder for its suffixes, so it gets the same check. - Three tests in the module's `mod tests` cover a negative length, a length past the end of the data, and a negative suffix length. `get` and `skip` are unchanged. The checks in `set_data` make their offsets valid. # Are these changes tested? The three new tests fail without this change and pass with it. The decoding module tests, clippy with warnings denied, the rustfmt check, and the arrow-feature type check all passed on this commit. Not run: the typos spell check, and the full parquet test suite. Only the decoding module tests were run. # Are there any significant user-facing changes? No public API change. Malformed pages that used to panic now return a ParquetError. # AI usage disclosure I used an AI coding assistant while preparing this change; I reviewed and tested all of it myself. --- parquet/src/encodings/decoding.rs | 76 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/parquet/src/encodings/decoding.rs b/parquet/src/encodings/decoding.rs index 54ff3db1f4..1de2844793 100644 --- a/parquet/src/encodings/decoding.rs +++ b/parquet/src/encodings/decoding.rs @@ -976,7 +976,24 @@ impl<T: DataType> Decoder<T> for DeltaLengthByteArrayDecoder<T> { self.lengths.resize(num_lengths, 0); len_decoder.get(&mut self.lengths[..])?; - self.data = Some(data.slice(len_decoder.get_offset()..)); + let data = data.slice(len_decoder.get_offset()..); + let mut total_bytes = 0usize; + for l in &self.lengths { + let len = usize::try_from(*l) + .map_err(|_| general_err!("Invalid DELTA_LENGTH_BYTE_ARRAY length {l}"))?; + total_bytes = total_bytes + .checked_add(len) + .ok_or_else(|| general_err!("DELTA_LENGTH_BYTE_ARRAY length overflow"))?; + } + if total_bytes > data.len() { + return Err(general_err!( + "Insufficient DELTA_LENGTH_BYTE_ARRAY data: lengths need {} bytes, page has {}", + total_bytes, + data.len() + )); + } + + self.data = Some(data); self.offset = 0; self.current_idx = 0; self.num_values = num_lengths; @@ -1026,6 +1043,7 @@ impl<T: DataType> Decoder<T> for DeltaLengthByteArrayDecoder<T> { Type::BYTE_ARRAY => { let num_values = cmp::min(num_values, self.num_values); + // set_data checked the lengths are nonnegative and fit in the page, so no overflow. let next_offset: i32 = self.lengths [self.current_idx..self.current_idx + num_values] .iter() @@ -1281,6 +1299,62 @@ mod tests { ); } + fn encode_delta_binary_packed(values: &[i32]) -> Vec<u8> { + let mut encoder = get_encoder::<Int32Type>( + Encoding::DELTA_BINARY_PACKED, + &create_test_col_desc_ptr(-1, Type::INT32), + ) + .unwrap(); + encoder.put(values).unwrap(); + encoder.flush_buffer().unwrap().as_ref().to_vec() + } + + #[test] + fn test_delta_length_byte_array_negative_length() { + let mut page = encode_delta_binary_packed(&[1, -1]); + page.extend_from_slice(b"ab"); + + let mut decoder = DeltaLengthByteArrayDecoder::<ByteArrayType>::new(); + let err = decoder.set_data(Bytes::from(page), 2).unwrap_err(); + assert!( + err.to_string() + .contains("Invalid DELTA_LENGTH_BYTE_ARRAY length"), + "{}", + err + ); + } + + #[test] + fn test_delta_length_byte_array_length_past_end() { + let mut page = encode_delta_binary_packed(&[1, 5]); + page.extend_from_slice(b"ab"); + + let mut decoder = DeltaLengthByteArrayDecoder::<ByteArrayType>::new(); + let err = decoder.set_data(Bytes::from(page), 2).unwrap_err(); + assert!( + err.to_string() + .contains("Insufficient DELTA_LENGTH_BYTE_ARRAY"), + "{}", + err + ); + } + + #[test] + fn test_delta_byte_array_negative_suffix_length() { + let mut page = encode_delta_binary_packed(&[0, 0]); + page.extend_from_slice(&encode_delta_binary_packed(&[1, -1])); + page.extend_from_slice(b"ab"); + + let mut decoder = DeltaByteArrayDecoder::<ByteArrayType>::new(); + let err = decoder.set_data(Bytes::from(page), 2).unwrap_err(); + assert!( + err.to_string() + .contains("Invalid DELTA_LENGTH_BYTE_ARRAY length"), + "{}", + err + ); + } + #[test] fn test_get_decoders() { // supported encodings
