diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc index 9abe41b4c3a3..4bb2293a3742 100644 --- a/cpp/src/arrow/buffer.cc +++ b/cpp/src/arrow/buffer.cc @@ -164,7 +164,9 @@ std::shared_ptr SliceMutableBuffer(std::shared_ptr buffer, MutableBuffer::MutableBuffer(const std::shared_ptr& parent, const int64_t offset, const int64_t size) - : MutableBuffer(reinterpret_cast(parent->mutable_address()) + offset, + : MutableBuffer(parent->mutable_address() + ? reinterpret_cast(parent->mutable_address() + offset) + : nullptr, size) { DCHECK(parent->is_mutable()) << "Must pass mutable buffer"; parent_ = parent; diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h index 388829eba05a..54837dfb4d8b 100644 --- a/cpp/src/arrow/buffer.h +++ b/cpp/src/arrow/buffer.h @@ -122,7 +122,7 @@ class ARROW_EXPORT Buffer { /// in general we expected buffers to be aligned and padded to 64 bytes. In the future /// we might add utility methods to help determine if a buffer satisfies this contract. Buffer(std::shared_ptr parent, const int64_t offset, const int64_t size) - : Buffer(parent->data_ + offset, size) { + : Buffer(parent->data_ ? parent->data_ + offset : nullptr, size) { parent_ = std::move(parent); SetMemoryManager(parent_->memory_manager_); } diff --git a/cpp/src/arrow/buffer_test.cc b/cpp/src/arrow/buffer_test.cc index bdda0b4a7476..7251187ffbaf 100644 --- a/cpp/src/arrow/buffer_test.cc +++ b/cpp/src/arrow/buffer_test.cc @@ -516,6 +516,18 @@ TEST(TestBuffer, ToHexString) { ASSERT_EQ(buf.ToHexString(), std::string("073068657820737472696E67A9")); } +TEST(TestBuffer, SliceNullBuffer) { + auto null_buffer = std::make_shared(nullptr, 16); + auto sliced = SliceBuffer(null_buffer, 8, 8); + EXPECT_EQ(sliced->data(), nullptr); + EXPECT_EQ(sliced->size(), 8); + + auto null_mutable = std::make_shared(nullptr, 16); + auto sliced_mutable = SliceMutableBuffer(null_mutable, 8, 8); + EXPECT_EQ(sliced_mutable->mutable_data(), nullptr); + EXPECT_EQ(sliced_mutable->size(), 8); +} + TEST(TestBuffer, SliceBuffer) { std::string data_str = "some data to slice"; auto data = reinterpret_cast(data_str.c_str());