This is an automated email from the ASF dual-hosted git repository.

pitrou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 55803660f01 GH-50314: [C++][Parquet] Reject invalid 
DELTA_BINARY_PACKED headers (#51128)
55803660f01 is described below

commit 55803660f015f9945fe1b9bc84f0c138ae70ce98
Author: Stefan Wang <[email protected]>
AuthorDate: Mon Nov 16 11:38:22 2026 -0500

    GH-50314: [C++][Parquet] Reject invalid DELTA_BINARY_PACKED headers (#51128)
    
    ### Rationale for this change
    
    A corrupt Parquet page can trigger a large scratch allocation from its 
header, even when it contains only one value.
    
    Fixes https://github.com/apache/arrow/issues/50314.
    
    ### What changes are included in this PR?
    
    Before allocating, the decoder checks that the remaining input has at least 
one bit-width byte per required miniblock. Block decoding stays separate from 
this header check. Single-value pages skip the unused buffer.
    
    ### Are these changes tested?
    
    Yes, by existing and new unit tests.
    
    ### Are there any user-facing changes?
    
    Valid pages decode as before. Headers exceeding the input bound fail before 
allocation; truncated block data still produces the decoder's EOF error.
    
    ### Was AI used for this PR?
    
    In accordance to the [AI generation 
guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
 please disclose below whether and how AI was used in this PR.
    
    **PR code and description written by:**
    
    - [ ] Human
    - [x] AI
    
    **Reviewed before submission by:**
    
    - [ ] Human
    - [x] AI
    - [ ] Not reviewed
    
    * GitHub Issue: #50314
    
    Lead-authored-by: 1fanwang <[email protected]>
    Co-authored-by: Antoine Pitrou <[email protected]>
    Signed-off-by: Antoine Pitrou <[email protected]>
---
 cpp/src/parquet/decoder.cc       | 26 +++++++++---
 cpp/src/parquet/encoding_test.cc | 89 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 5 deletions(-)

diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc
index 6832d93507c..076a44cfc0b 100644
--- a/cpp/src/parquet/decoder.cc
+++ b/cpp/src/parquet/decoder.cc
@@ -1613,11 +1613,27 @@ class DeltaBitPackDecoder : public 
TypedDecoderImpl<DType> {
     }
 
     total_values_remaining_ = total_value_count_;
-    if (delta_bit_widths_ == nullptr) {
-      delta_bit_widths_ = AllocateBuffer(pool_, mini_blocks_per_block_);
-    } else {
-      PARQUET_THROW_NOT_OK(
-          delta_bit_widths_->Resize(mini_blocks_per_block_, /*shrink_to_fit*/ 
false));
+    // A scratch space for delta bit widths is only necessary if there is more
+    // than 1 value, since the first value is part of the header.
+    if (total_value_count_ > 1) {
+      const int64_t required_blocks =
+          ::arrow::bit_util::CeilDiv(total_value_count_ - 1, 
values_per_block_);
+      const int64_t required_bit_widths =
+          required_blocks * static_cast<int64_t>(mini_blocks_per_block_);
+      const int64_t bytes_left = decoder_->bytes_left();
+      // GH-50314: avoid excessive memory allocation in case of corrupt header.
+      if (required_bit_widths > bytes_left) {
+        throw ParquetException("the required number of miniblock bit widths (" 
+
+                               std::to_string(required_bit_widths) +
+                               ") exceeds the number of bytes remaining in the 
data (" +
+                               std::to_string(bytes_left) + ")");
+      }
+      if (delta_bit_widths_ == nullptr) {
+        delta_bit_widths_ = AllocateBuffer(pool_, mini_blocks_per_block_);
+      } else {
+        PARQUET_THROW_NOT_OK(
+            delta_bit_widths_->Resize(mini_blocks_per_block_, 
/*shrink_to_fit*/ false));
+      }
     }
     first_block_initialized_ = false;
     values_remaining_current_mini_block_ = 0;
diff --git a/cpp/src/parquet/encoding_test.cc b/cpp/src/parquet/encoding_test.cc
index 672e55a900d..f378812d8c6 100644
--- a/cpp/src/parquet/encoding_test.cc
+++ b/cpp/src/parquet/encoding_test.cc
@@ -32,6 +32,7 @@
 #include "arrow/array/builder_dict.h"
 #include "arrow/array/concatenate.h"
 #include "arrow/compute/cast.h"
+#include "arrow/memory_pool.h"
 #include "arrow/testing/gtest_util.h"
 #include "arrow/testing/random.h"
 #include "arrow/testing/util.h"
@@ -1907,6 +1908,94 @@ TYPED_TEST(TestDeltaBitPackEncoding, BasicRoundTrip) {
   }
 }
 
+TYPED_TEST(TestDeltaBitPackEncoding, SingleValueSkipsMiniblockAllocation) {
+  using T = typename TypeParam::c_type;
+
+  // Header: 2^25 values per block, 2^20 miniblocks, 1 value, and first value 
0.
+  const std::vector<uint8_t> encoded = {0x80, 0x80, 0x80, 0x10, 0x80,
+                                        0x80, 0x40, 0x01, 0x00};
+  ::arrow::ProxyMemoryPool pool(default_memory_pool());
+  auto decoder = MakeTypedDecoder<TypeParam>(Encoding::DELTA_BINARY_PACKED,
+                                             this->descr_.get(), &pool);
+  T decoded = 1;
+
+  decoder->SetData(1, encoded.data(), static_cast<int>(encoded.size()));
+  ASSERT_EQ(decoder->Decode(&decoded, 1), 1);
+  EXPECT_EQ(decoded, 0);
+  EXPECT_EQ(pool.total_bytes_allocated(), 0);
+}
+
+TYPED_TEST(TestDeltaBitPackEncoding, RejectsMiniblockWidthsLargerThanInput) {
+  using T = typename TypeParam::c_type;
+
+  // Header: 2^25 values per block, 2^20 miniblocks, 2 values, and first value 
0,
+  // followed by min delta 0 and no miniblock bit widths.
+  const std::vector<uint8_t> encoded = {0x80, 0x80, 0x80, 0x10, 0x80,
+                                        0x80, 0x40, 0x02, 0x00, 0x00};
+  ::arrow::ProxyMemoryPool pool(default_memory_pool());
+  auto decoder = MakeTypedDecoder<TypeParam>(Encoding::DELTA_BINARY_PACKED,
+                                             this->descr_.get(), &pool);
+  std::vector<T> decoded(2);
+
+  EXPECT_THROW_THAT(
+      [&] {
+        decoder->SetData(2, encoded.data(), static_cast<int>(encoded.size()));
+        decoder->Decode(decoded.data(), static_cast<int>(decoded.size()));
+      },
+      ParquetException,
+      ::testing::Property(
+          &ParquetException::what,
+          ::testing::HasSubstr("the required number of miniblock bit widths "
+                               "(1048576) exceeds the number of bytes 
remaining "
+                               "in the data (1)")));
+  EXPECT_EQ(pool.total_bytes_allocated(), 0);
+}
+
+TYPED_TEST(TestDeltaBitPackEncoding, RejectsInsufficientBitWidthsAcrossBlocks) 
{
+  using T = typename TypeParam::c_type;
+
+  // Header: 128 values per block, 1 miniblock, 130 values, and first value 0.
+  const std::vector<uint8_t> encoded = {0x80, 0x01, 0x01, 0x82, 0x01, 0x00, 
0x00};
+  ::arrow::ProxyMemoryPool pool(default_memory_pool());
+  auto decoder = MakeTypedDecoder<TypeParam>(Encoding::DELTA_BINARY_PACKED,
+                                             this->descr_.get(), &pool);
+  std::vector<T> decoded(130);
+
+  EXPECT_THROW_THAT(
+      [&] {
+        decoder->SetData(static_cast<int>(decoded.size()), encoded.data(),
+                         static_cast<int>(encoded.size()));
+        decoder->Decode(decoded.data(), static_cast<int>(decoded.size()));
+      },
+      ParquetException,
+      ::testing::Property(
+          &ParquetException::what,
+          ::testing::HasSubstr("the required number of miniblock bit widths 
(2) "
+                               "exceeds the number of bytes remaining in the "
+                               "data (1)")));
+  EXPECT_EQ(pool.total_bytes_allocated(), 0);
+}
+
+TYPED_TEST(TestDeltaBitPackEncoding, 
RejectsMissingBitWidthAfterMultiByteMinDelta) {
+  using T = typename TypeParam::c_type;
+
+  // Header: 128 values per block, 1 miniblock, 2 values, and first value 0,
+  // followed by a two-byte min delta and no miniblock bit-width byte.
+  const std::vector<uint8_t> encoded = {0x80, 0x01, 0x01, 0x02, 0x00, 0x80, 
0x01};
+  auto decoder =
+      MakeTypedDecoder<TypeParam>(Encoding::DELTA_BINARY_PACKED, 
this->descr_.get());
+  std::vector<T> decoded(2);
+
+  EXPECT_THROW_THAT(
+      [&] {
+        decoder->SetData(2, encoded.data(), static_cast<int>(encoded.size()));
+        decoder->Decode(decoded.data(), static_cast<int>(decoded.size()));
+      },
+      ParquetException,
+      ::testing::Property(&ParquetException::what,
+                          ::testing::HasSubstr("Decode bit-width EOF")));
+}
+
 TYPED_TEST(TestDeltaBitPackEncoding, NonZeroPaddedMiniblockBitWidth) {
   // GH-14923: depending on the number of encoded values, some of the miniblock
   // bitwidths are actually padding bytes that may take non-conformant values

Reply via email to