This is an automated email from the ASF dual-hosted git repository.

wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new d0e926010 fix(blob): allow `blob` object to be initialized with 
null-pointer char array whose length must be zero (#2110)
d0e926010 is described below

commit d0e92601011b65e7562d5d9ba464e2a2f848172e
Author: Dan Wang <[email protected]>
AuthorDate: Fri Sep 6 11:04:54 2024 +0800

    fix(blob): allow `blob` object to be initialized with null-pointer char 
array whose length must be zero (#2110)
    
    Fix https://github.com/apache/incubator-pegasus/issues/2109.
    
    Null-pointer char array with zero length is allowed to be used to initialize
    `blob` object. Use `DCHECK` instead to check if the pointer is valid while
    initializing `blob` object.
---
 src/utils/blob.h             |  4 +++-
 src/utils/test/blob_test.cpp | 21 ++++++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/utils/blob.h b/src/utils/blob.h
index 62bc0a8a5..07675e943 100644
--- a/src/utils/blob.h
+++ b/src/utils/blob.h
@@ -109,7 +109,9 @@ public:
     /// NOTE: this operation is not efficient since it involves a memory copy.
     [[nodiscard]] static blob create_from_bytes(const char *s, size_t len)
     {
-        CHECK_NOTNULL(s, "null source pointer would lead to undefined 
behaviour");
+        DCHECK(s != nullptr || len == 0,
+               "null source pointer with non-zero length would lead to "
+               "undefined behaviour");
 
         std::shared_ptr<char> s_arr(new char[len], 
std::default_delete<char[]>());
         memcpy(s_arr.get(), s, len);
diff --git a/src/utils/test/blob_test.cpp b/src/utils/test/blob_test.cpp
index 8ae2c8b7f..586a7d68d 100644
--- a/src/utils/test/blob_test.cpp
+++ b/src/utils/test/blob_test.cpp
@@ -24,6 +24,25 @@
 
 namespace dsn {
 
+TEST(BlobTest, CreateFromZeroLengthNullptr)
+{
+    const auto &obj = blob::create_from_bytes(nullptr, 0);
+
+    EXPECT_EQ(0, obj.length());
+    EXPECT_EQ(0, obj.size());
+}
+
+#ifndef NDEBUG
+
+TEST(BlobTest, CreateFromNonZeroLengthNullptr)
+{
+    ASSERT_DEATH({ const auto &obj = blob::create_from_bytes(nullptr, 1); },
+                 "null source pointer with non-zero length would lead to "
+                 "undefined behaviour");
+}
+
+#endif
+
 struct blob_base_case
 {
     std::string expected_str;
@@ -32,7 +51,7 @@ struct blob_base_case
 class BlobBaseTest : public testing::TestWithParam<blob_base_case>
 {
 public:
-    void SetUp() override
+    BlobBaseTest()
     {
         const auto &test_case = GetParam();
         _expected_str = test_case.expected_str;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to