Skip to content

Commit

Permalink
[#23960] DocDB: Fix WriteBuffer crash in move ctor
Browse files Browse the repository at this point in the history
Summary:
WriteBuffer move ctor does not initialize consumption_.
So segmentation fault could happen when WriteBuffer is constucted over non zeroed memory.
Jira: DB-12860

Test Plan: WriteBufferTest.MoveCtor

Reviewers: rthallam, esheng

Reviewed By: esheng

Subscribers: ybase

Differential Revision: https://phorge.dev.yugabyte.com/D38158
  • Loading branch information
spolitov committed Sep 19, 2024
1 parent aa14f76 commit 885b72b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/yb/util/write_buffer-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ TEST(WriteBufferTest, AppendPerformance) {
});
}

TEST(WriteBufferTest, MoveCtor) {
std::string text("Hello");
WriteBuffer original(0x10);
original.Append(text.c_str(), text.size());
alignas(alignof(WriteBuffer)) char buffer[sizeof(WriteBuffer)];
std::fill_n(buffer, sizeof (buffer), 0x40);
auto* moved = new (buffer) WriteBuffer(std::move(original));
ASSERT_EQ(moved->ToBuffer(), text);
moved->~WriteBuffer();
}

} // namespace yb
5 changes: 4 additions & 1 deletion src/yb/util/write_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class WriteBuffer {
explicit WriteBuffer(size_t block_size, ScopedTrackedConsumption* consumption = nullptr)
: block_size_(block_size), consumption_(consumption) {}

WriteBuffer(WriteBuffer&& rhs) : block_size_(rhs.block_size_) {
WriteBuffer(WriteBuffer&& rhs) : block_size_(rhs.block_size_), consumption_(nullptr) {
auto consumption = rhs.consumption_;
rhs.consumption_ = nullptr;
Take(&rhs);
consumption_ = consumption;
}

void operator=(WriteBuffer&& rhs) {
Expand Down

0 comments on commit 885b72b

Please sign in to comment.