Skip to content
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

GH-39126: [C++][CI] Fix Valgrind failures #39127

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/src/arrow/array/array_dict_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ TEST(TestDictionary, Validate) {
arr = std::make_shared<DictionaryArray>(dict_type, indices, MakeArray(invalid_data));
ASSERT_RAISES(Invalid, arr->ValidateFull());

#if !defined(__APPLE__)
#if !defined(__APPLE__) && !defined(ARROW_VALGRIND)
// GH-35712: ASSERT_DEATH would make testing slow on MacOS.
ASSERT_DEATH(
{
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ TEST_F(TestArray, TestMakeArrayFromScalar) {
}

for (auto scalar : scalars) {
ARROW_SCOPED_TRACE("scalar type: ", scalar->type->ToString());
AssertAppendScalar(pool_, scalar);
}
}
Expand Down
9 changes: 5 additions & 4 deletions cpp/src/arrow/array/builder_binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ Status BinaryViewBuilder::AppendArraySlice(const ArraySpan& array, int64_t offse
Status BinaryViewBuilder::FinishInternal(std::shared_ptr<ArrayData>* out) {
ARROW_ASSIGN_OR_RAISE(auto null_bitmap, null_bitmap_builder_.FinishWithLength(length_));
ARROW_ASSIGN_OR_RAISE(auto data, data_builder_.FinishWithLength(length_));
BufferVector buffers = {null_bitmap, data};
for (auto&& buffer : data_heap_builder_.Finish()) {
buffers.push_back(std::move(buffer));
}
ARROW_ASSIGN_OR_RAISE(auto byte_buffers, data_heap_builder_.Finish());
BufferVector buffers(byte_buffers.size() + 2);
buffers[0] = std::move(null_bitmap);
buffers[1] = std::move(data);
std::move(byte_buffers.begin(), byte_buffers.end(), byte_buffers.begin() + 2);
pitrou marked this conversation as resolved.
Show resolved Hide resolved
*out = ArrayData::Make(type(), length_, std::move(buffers), null_count_);
Reset();
return Status::OK();
Expand Down
31 changes: 20 additions & 11 deletions cpp/src/arrow/array/builder_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,11 @@ class ARROW_EXPORT StringHeapBuilder {
"strings larger than 2GB");
}
if (num_bytes > current_remaining_bytes_) {
// Ensure the buffer is fully overwritten to avoid leaking uninitialized
// bytes from the allocator
if (current_remaining_bytes_ > 0) {
std::memset(current_out_buffer_, 0, current_remaining_bytes_);
blocks_.back() = SliceBuffer(blocks_.back(), 0,
blocks_.back()->size() - current_remaining_bytes_);
}
ARROW_RETURN_NOT_OK(FinishLastBlock());
current_remaining_bytes_ = num_bytes > blocksize_ ? num_bytes : blocksize_;
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> new_block,
AllocateBuffer(current_remaining_bytes_, alignment_, pool_));
ARROW_ASSIGN_OR_RAISE(
std::shared_ptr<ResizableBuffer> new_block,
AllocateResizableBuffer(current_remaining_bytes_, alignment_, pool_));
current_offset_ = 0;
current_out_buffer_ = new_block->mutable_data();
blocks_.emplace_back(std::move(new_block));
Expand All @@ -550,18 +545,32 @@ class ARROW_EXPORT StringHeapBuilder {

int64_t current_remaining_bytes() const { return current_remaining_bytes_; }

std::vector<std::shared_ptr<Buffer>> Finish() {
Result<std::vector<std::shared_ptr<ResizableBuffer>>> Finish() {
if (!blocks_.empty()) {
ARROW_RETURN_NOT_OK(FinishLastBlock());
}
current_offset_ = 0;
current_out_buffer_ = NULLPTR;
current_remaining_bytes_ = 0;
return std::move(blocks_);
}

private:
Status FinishLastBlock() {
if (current_remaining_bytes_ > 0) {
// Avoid leaking uninitialized bytes from the allocator
ARROW_RETURN_NOT_OK(
blocks_.back()->Resize(blocks_.back()->size() - current_remaining_bytes_,
/*shrink_to_fit=*/true));
blocks_.back()->ZeroPadding();
}
return Status::OK();
}

MemoryPool* pool_;
int64_t alignment_;
int64_t blocksize_ = kDefaultBlocksize;
std::vector<std::shared_ptr<Buffer>> blocks_;
std::vector<std::shared_ptr<ResizableBuffer>> blocks_;

int32_t current_offset_ = 0;
uint8_t* current_out_buffer_ = NULLPTR;
Expand Down
Loading