Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Sep 5, 2024
1 parent 8027682 commit 146b6b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
12 changes: 3 additions & 9 deletions src/utils/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,9 @@ class blob
/// NOTE: this operation is not efficient since it involves a memory copy.
[[nodiscard]] static blob create_from_bytes(const char *s, size_t len)
{
if (s == nullptr) {
// TODO(wangdan): decide if an empty blob could be returned, which is initialized
// by default constructor blob() whose length is zero and all pointers(_holder,
// _buffer and _data) are null.
CHECK_EQ_MSG(len,
0,
"null source pointer with non-zero length 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);
Expand Down
15 changes: 12 additions & 3 deletions src/utils/test/blob_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@

namespace dsn {

TEST(BlobTest, CreateFromNullptr)
TEST(BlobTest, CreateFromZeroLengthNullptr)
{
const auto &obj = blob::create_from_bytes(nullptr, 0);

// TODO(wangdan): once create_from_bytes() could return an empty blob, add case
// to test if all pointers are null.
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;
Expand Down

0 comments on commit 146b6b5

Please sign in to comment.