SteNicholas commented on code in PR #514:
URL: https://github.com/apache/paimon-cpp/pull/514#discussion_r4476256128


##########
src/paimon/common/memory/memory_segment_test.cpp:
##########
@@ -675,6 +675,20 @@ TEST(MemorySegmentTest, TestEqual) {
     ASSERT_FALSE(seg2 == seg1);
 }
 
+TEST(MemorySegmentTest, TestGetOrCreateHeapMemoryForEmptySegment) {
+    auto pool = paimon::GetDefaultPool();
+    auto view = MemorySegment::WrapView("", 0);
+    auto heap = view.GetOrCreateHeapMemory(pool.get());
+    ASSERT_TRUE(heap);
+    ASSERT_EQ(0u, heap->size());
+
+    auto owning = MemorySegment::Wrap(heap);
+    ASSERT_EQ(heap, owning.GetOrCreateHeapMemory(pool.get()));
+
+    auto null_view = MemorySegment::WrapView(nullptr, 0);
+    ASSERT_EQ(nullptr, null_view.GetOrCreateHeapMemory(pool.get()));

Review Comment:
   [P3, documentation] Could the public GetOrCreateHeapMemory() method document 
the empty-segment cases captured by this test? An owning empty segment returns 
its existing Bytes object, WrapView("", 0) produces a non-null empty Bytes 
object, and WrapView(nullptr, 0) returns nullptr. Callers need that distinction 
to know whether they can dereference the result. A short Doxygen return-value 
description in memory_segment.h would make this contract explicit.



##########
src/paimon/common/io/byte_array_input_stream_test.cpp:
##########
@@ -81,4 +81,53 @@ TEST(ByteArrayInputStreamTest, TestSimple) {
     ASSERT_OK(input_stream->Close());
 }
 
+TEST(ByteArrayInputStreamTest, TestZeroLengthRead) {
+    auto pool = GetDefaultPool();
+    Bytes empty_bytes(0, pool.get());
+    ByteArrayInputStream input_stream("abc", 3);
+    ASSERT_OK(input_stream.Seek(1, SeekOrigin::FS_SEEK_SET));
+
+    ASSERT_OK_AND_ASSIGN(int64_t read_size, 
input_stream.Read(empty_bytes.data(), 0));
+    ASSERT_EQ(0, read_size);
+    ASSERT_OK_AND_ASSIGN(int64_t position, input_stream.GetPos());
+    ASSERT_EQ(1, position);
+
+    ASSERT_OK_AND_ASSIGN(read_size, input_stream.Read(empty_bytes.data(), 0, 
/*offset=*/0));
+    ASSERT_EQ(0, read_size);
+    ASSERT_OK_AND_ASSIGN(read_size, input_stream.Read(empty_bytes.data(), 0, 
/*offset=*/3));
+    ASSERT_EQ(0, read_size);
+    ASSERT_OK_AND_ASSIGN(position, input_stream.GetPos());
+    ASSERT_EQ(1, position);
+
+    bool read_finished = false;
+    input_stream.ReadAsync(empty_bytes.data(), 0, /*offset=*/3, [&](Status 
status) {
+        ASSERT_OK(status);
+        read_finished = true;
+    });
+    ASSERT_TRUE(read_finished);

Review Comment:
   [P3, non-blocking] Consider waiting for the callback result through a 
promise/future before checking the status and stream position. This test 
currently passes because ByteArrayInputStream invokes its callback 
synchronously. InputStream::ReadAsync only promises a callback on completion, 
so a later implementation that dispatches the callback would make the immediate 
read_finished assertion fail and could access the captured local after the test 
returns. Synchronizing completion would let the test verify successful 
zero-length reads and position preservation without relying on inline callback 
execution.



##########
src/paimon/common/memory/memory_slice_test.cpp:
##########
@@ -275,6 +275,20 @@ TEST_F(MemorySliceTest, TestSliceCopyBytes) {
     ASSERT_NE(bytes->data(), copied->data());
 }
 
+TEST_F(MemorySliceTest, TestSliceCopyEmptyBytes) {
+    auto bytes = std::make_shared<Bytes>(0, pool_.get());
+    MemorySlice slice = MemorySlice::Wrap(bytes);
+    auto copied = slice.CopyBytes(pool_.get());
+    ASSERT_TRUE(copied);
+    ASSERT_EQ(0u, copied->size());

Review Comment:
   [P2] Please make the sanitizer regression checks fail when UBSan detects the 
original bug. These assertions also pass with the old implementation. I ran all 
four new tests against the base implementation in a macOS control build with 
explicit nonnull annotations on memcpy. UBSan reported the null arguments at 
all four copy sites, yet GoogleTest reported 4 passed and the process exited 
with 0. With UBSAN_OPTIONS=halt_on_error=1, the corresponding base tests failed 
and the PR version passed all four tests. The current build_support/run-test.sh 
enables stack traces and suppressions, but leaves these UBSan reports 
recoverable. Please run the focused tests with halt_on_error=1 in the Linux 
sanitizer job, or use an equivalent fatal check, and verify that reverting the 
guards makes that check fail.



##########
src/paimon/common/memory/memory_slice.cpp:
##########
@@ -78,7 +78,9 @@ std::string_view MemorySlice::ReadStringView() const {
 
 std::shared_ptr<Bytes> MemorySlice::CopyBytes(MemoryPool* pool) const {
     auto bytes = std::make_shared<Bytes>(length_, pool);
-    std::memcpy(const_cast<char*>(bytes->data()), Data(), length_);
+    if (length_ > 0) {
+        std::memcpy(const_cast<char*>(bytes->data()), Data(), length_);

Review Comment:
   [P3, cleanup] Bytes::data() const already returns char*, so 
const_cast<char*> is redundant here. Please pass bytes->data() directly. The 
same cleanup applies to copy->data() in the modified 
MemorySegment::GetOrCreateHeapMemory() call. This makes the destination's 
mutability clear without suggesting that the copy requires removing constness.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to