aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluigi1111 <luigi1111w@gmail.com>2020-05-14 16:10:59 -0500
committerluigi1111 <luigi1111w@gmail.com>2020-05-14 16:10:59 -0500
commita67a9fc9c4d4a1f896493031308093eb0ec6c9ac (patch)
tree180630f5de1213054a023cd3754f733fed569227
parentMerge pull request #6520 (diff)
parentFixed bugs for take_slice and byte_stream->byte_slice (diff)
downloadmonero-a67a9fc9c4d4a1f896493031308093eb0ec6c9ac.tar.xz
Merge pull request #6524
cc40ce1 Fixed bugs for take_slice and byte_stream->byte_slice (vtnerd)
-rw-r--r--contrib/epee/src/byte_slice.cpp26
-rw-r--r--tests/unit_tests/epee_utils.cpp31
2 files changed, 48 insertions, 9 deletions
diff --git a/contrib/epee/src/byte_slice.cpp b/contrib/epee/src/byte_slice.cpp
index 99c37fae3..12cc83e6c 100644
--- a/contrib/epee/src/byte_slice.cpp
+++ b/contrib/epee/src/byte_slice.cpp
@@ -173,9 +173,14 @@ namespace epee
byte_slice::byte_slice(byte_stream&& stream) noexcept
: storage_(nullptr), portion_(stream.data(), stream.size())
{
- std::uint8_t* const data = stream.take_buffer().release() - sizeof(raw_byte_slice);
- new (data) raw_byte_slice{};
- storage_.reset(reinterpret_cast<raw_byte_slice*>(data));
+ if (stream.size())
+ {
+ std::uint8_t* const data = stream.take_buffer().release() - sizeof(raw_byte_slice);
+ new (data) raw_byte_slice{};
+ storage_.reset(reinterpret_cast<raw_byte_slice*>(data));
+ }
+ else
+ portion_ = nullptr;
}
byte_slice::byte_slice(byte_slice&& source) noexcept
@@ -205,14 +210,17 @@ namespace epee
byte_slice byte_slice::take_slice(const std::size_t max_bytes) noexcept
{
byte_slice out{};
- std::uint8_t const* const ptr = data();
- out.portion_ = {ptr, portion_.remove_prefix(max_bytes)};
- if (portion_.empty())
- out.storage_ = std::move(storage_); // no atomic inc/dec
- else
- out = {storage_.get(), out.portion_};
+ if (max_bytes)
+ {
+ std::uint8_t const* const ptr = data();
+ out.portion_ = {ptr, portion_.remove_prefix(max_bytes)};
+ if (portion_.empty())
+ out.storage_ = std::move(storage_); // no atomic inc/dec
+ else
+ out = {storage_.get(), out.portion_};
+ }
return out;
}
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index 2e93f9e67..a2cec965e 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -667,6 +667,23 @@ TEST(ByteSlice, TakeSlice)
EXPECT_TRUE(boost::range::equal(base_string, slice));
const epee::span<const std::uint8_t> original = epee::to_span(slice);
+ const epee::byte_slice empty_slice = slice.take_slice(0);
+ EXPECT_EQ(original.begin(), slice.begin());
+ EXPECT_EQ(slice.begin(), slice.cbegin());
+ EXPECT_EQ(original.end(), slice.end());
+ EXPECT_EQ(slice.end(), slice.cend());
+
+ EXPECT_EQ(nullptr, empty_slice.begin());
+ EXPECT_EQ(nullptr, empty_slice.cbegin());
+ EXPECT_EQ(nullptr, empty_slice.end());
+ EXPECT_EQ(nullptr, empty_slice.cend());
+ EXPECT_EQ(nullptr, empty_slice.data());
+ EXPECT_TRUE(empty_slice.empty());
+ EXPECT_EQ(0u, empty_slice.size());
+
+ EXPECT_FALSE(slice.empty());
+ EXPECT_EQ(slice.cbegin(), slice.data());
+
const epee::byte_slice slice2 = slice.take_slice(remove_size);
EXPECT_EQ(original.begin() + remove_size, slice.begin());
@@ -1061,6 +1078,20 @@ TEST(ByteStream, ToByteSlice)
EXPECT_EQ(nullptr, stream.data());
EXPECT_EQ(nullptr, stream.tellp());
EXPECT_TRUE(equal(source, slice));
+
+ stream = epee::byte_stream{};
+ stream.reserve(1);
+ EXPECT_NE(nullptr, stream.data());
+ EXPECT_NE(nullptr, stream.tellp());
+
+ const epee::byte_slice empty_slice{std::move(stream)};
+ EXPECT_TRUE(empty_slice.empty());
+ EXPECT_EQ(0u, empty_slice.size());
+ EXPECT_EQ(nullptr, empty_slice.begin());
+ EXPECT_EQ(nullptr, empty_slice.cbegin());
+ EXPECT_EQ(nullptr, empty_slice.end());
+ EXPECT_EQ(nullptr, empty_slice.cend());
+ EXPECT_EQ(nullptr, empty_slice.data());
}
TEST(ToHex, String)