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

maplefu 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 501418ece4 GH-44690: [C++] NumericBuilder::AppendValues append vector 
prevent from ub (#44794)
501418ece4 is described below

commit 501418ece4392b8059151b0d4c3ea174ab2996a2
Author: mwish <[email protected]>
AuthorDate: Wed Nov 20 22:32:02 2024 +0800

    GH-44690: [C++] NumericBuilder::AppendValues append vector prevent from ub 
(#44794)
    
    
    
    ### Rationale for this change
    
    Add boundary check for `NumericBuilder::AppendValues` for std::vector
    
    Originally, it will :
    
    1. `AppendValues` might has `std::vector` as arguments, `std::vector::data` 
might be used
    2. `std::vector::data` might returns `nullptr` if size == 0: 
https://en.cppreference.com/w/cpp/container/vector/data
    3. https://en.cppreference.com/w/cpp/string/byte/memcpy memcpy says, "If 
either dest or src is an [invalid or null 
pointer](https://en.cppreference.com/w/cpp/language/pointer#Pointers), the 
behavior is undefined, even if count is zero."
    
    ### What changes are included in this PR?
    
    Add boundary check for `NumericBuilder::AppendValues` for std::vector
    
    ### Are these changes tested?
    
    Covered by existing
    
    ### Are there any user-facing changes?
    
    no
    
    * GitHub Issue: #44690
    
    Authored-by: mwish <[email protected]>
    Signed-off-by: mwish <[email protected]>
---
 cpp/src/arrow/array/builder_primitive.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/cpp/src/arrow/array/builder_primitive.h 
b/cpp/src/arrow/array/builder_primitive.h
index de7af1b46b..be9761fb46 100644
--- a/cpp/src/arrow/array/builder_primitive.h
+++ b/cpp/src/arrow/array/builder_primitive.h
@@ -211,6 +211,9 @@ class NumericBuilder
   /// \return Status
   Status AppendValues(const std::vector<value_type>& values,
                       const std::vector<bool>& is_valid) {
+    if (values.empty()) {
+      return Status::OK();
+    }
     return AppendValues(values.data(), static_cast<int64_t>(values.size()), 
is_valid);
   }
 
@@ -218,6 +221,9 @@ class NumericBuilder
   /// \param[in] values a std::vector of values
   /// \return Status
   Status AppendValues(const std::vector<value_type>& values) {
+    if (values.empty()) {
+      return Status::OK();
+    }
     return AppendValues(values.data(), static_cast<int64_t>(values.size()));
   }
 

Reply via email to