Skip to content

Commit

Permalink
GH-44690: [C++] NumericBuilder::AppendValues append vector prevent fr…
Browse files Browse the repository at this point in the history
…om 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]>
  • Loading branch information
mapleFU authored Nov 20, 2024
1 parent 33e8cbb commit 501418e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cpp/src/arrow/array/builder_primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,19 @@ 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);
}

/// \brief Append a sequence of elements in one shot
/// \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()));
}

Expand Down

0 comments on commit 501418e

Please sign in to comment.