fanruan-tuco opened a new issue, #55628:
URL: https://github.com/apache/arrow/issues/55628

   In Arrow 16.1.0, a Parquet Double dictionary write can fail with OOM, then 
crash with SIGSEGV during writer destruction.
   
   **Allocation failure** — 
[`HashTable::Upsize()`](https://github.com/apache/arrow/blob/apache-arrow-16.1.0/cpp/src/arrow/util/hashing.h#L342)
 detaches the old buffer before allocating its replacement:
   
   ```cpp
   const Entry* old_entries = entries_;
   ARROW_ASSIGN_OR_RAISE(auto previous, 
entries_builder_.FinishWithLength(capacity_));
   RETURN_NOT_OK(UpsizeBuffer(new_capacity));
   ```
   
   If `entries_builder_.Resize()` inside `UpsizeBuffer()` fails, it returns 
before updating `entries_`.
   
   **Release** — `FinishWithLength()` resets the builder. On the error return, 
local `previous` is destroyed, releasing the old buffer through 
[`PoolBuffer::~PoolBuffer()`](https://github.com/apache/arrow/blob/apache-arrow-16.1.0/cpp/src/arrow/memory_pool.cc#L844):
   
   ```cpp
   pool_->Free(ptr, capacity_, alignment_);
   ```
   
   **Use after free** — `entries_` still points to the freed allocation. The 
file writer destructor calls `Close()`, and 
[`ColumnWriterImpl::Close()`](https://github.com/apache/arrow/blob/apache-arrow-16.1.0/cpp/src/parquet/column_writer.cc#L1089)
 still attempts to write the dictionary:
   
   ```cpp
   if (has_dictionary_ && !fallback_) {
     WriteDictionaryPage();
   }
   ```
   
   This reaches `WriteDict()` → `CopyValues()` → 
[`VisitEntries()`](https://github.com/apache/arrow/blob/apache-arrow-16.1.0/cpp/src/arrow/util/hashing.h#L275),
 which dereferences the dangling pointer:
   
   ```cpp
   const auto& entry = entries_[i];
   ```
   
   Expected: a failed write must not cause subsequent close/destruction to 
access freed memory or crash the process.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to