-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C++] Builder would ub when AppendValues
called with empty vector
#44690
Comments
I have zero knowledge here so bear with me if there is something obvious I am missing. I was trying to understand what the issue is and after some investigation it seems that memcpy should just handle the size 0 without problem, could you expand on what would be the undefined behavior?
|
@raulcd There is few steps for this problem
|
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()));
return AppendValues(values.data(), static_cast<int64_t>(values.size()));
} @pitrou @bkietz @felipecrv Should I just guard the vector interface? Or should prevent from all AppendValues? |
Can you submit a PR and we can review that? |
done #44794 |
…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]>
Issue resolved by pull request 44794 |
Describe the enhancement requested
When
AppendValues
called with empty vector, this would cause memcpyIn this case, unsafe append directly memcpy, and when size == 0, it would be a undefined behavior
Component(s)
C++
The text was updated successfully, but these errors were encountered: