Skip to content

Commit

Permalink
[ubsan](PODArray)Avoid applying non-zero offset to null pointer (apac…
Browse files Browse the repository at this point in the history
…he#41525)

The original code would generate a null c_end when there is no padding.
before
```
    static constexpr char* null =
            pad_left ? const_cast<char*>(empty_pod_array) + EmptyPODArraySize : nullptr;
```
now
```
    static constexpr char* null = const_cast<char*>(empty_pod_array) + pad_left;
```
  • Loading branch information
Mryange authored Oct 9, 2024
1 parent bcbec25 commit 4602068
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions be/src/vec/common/pod_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ class PODArrayBase : private boost::noncopyable,
/// pad_left is also rounded up to 16 bytes to maintain alignment of allocated memory.
static constexpr size_t pad_left = integerRoundUp(integerRoundUp(pad_left_, ELEMENT_SIZE), 16);
/// Empty array will point to this static memory as padding.
static constexpr char* null =
pad_left ? const_cast<char*>(empty_pod_array) + EmptyPODArraySize : nullptr;
static constexpr char* null = const_cast<char*>(empty_pod_array) + pad_left;

static_assert(pad_left <= EmptyPODArraySize &&
"Left Padding exceeds EmptyPODArraySize. Is the element size too large?");
Expand Down Expand Up @@ -403,7 +402,7 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r

template <typename U, typename... TAllocatorParams>
void push_back(U&& x, TAllocatorParams&&... allocator_params) {
if (UNLIKELY(this->c_end == nullptr || this->c_end + sizeof(T) > this->c_end_of_storage)) {
if (UNLIKELY(this->c_end + sizeof(T) > this->c_end_of_storage)) {
this->reserve_for_next_size(std::forward<TAllocatorParams>(allocator_params)...);
}

Expand Down Expand Up @@ -445,8 +444,7 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
*/
template <typename... Args>
void emplace_back(Args&&... args) {
if (UNLIKELY(this->c_end == nullptr ||
(this->c_end + sizeof(T) > this->c_end_of_storage))) {
if (UNLIKELY(this->c_end + sizeof(T) > this->c_end_of_storage)) {
this->reserve_for_next_size();
}

Expand Down

0 comments on commit 4602068

Please sign in to comment.