From 151acf120a734dc0806398aba548cbdf62197559 Mon Sep 17 00:00:00 2001 From: atharva-potdar Date: Sat, 7 Nov 2026 12:54:41 +0530 Subject: [PATCH 1/2] test(c++): add test for slicing null buffers --- cpp/src/arrow/buffer_test.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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()); From 0b02925b5d90a7309476793cb4f3bc64adeaf1b1 Mon Sep 17 00:00:00 2001 From: atharva-potdar Date: Sat, 7 Nov 2026 13:27:21 +0530 Subject: [PATCH 2/2] fix(c++): prevent null pointer arithmetic in Buffer and MutableBuffer slicing --- cpp/src/arrow/buffer.cc | 4 +++- cpp/src/arrow/buffer.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) 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_); }