aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluigi1111 <luigi1111w@gmail.com>2020-05-21 03:06:15 -0500
committerluigi1111 <luigi1111w@gmail.com>2020-05-21 03:06:15 -0500
commit4f6dbcd00e3c4b3edfefe4ad5c64172d37b5a011 (patch)
tree814282019f5234d06b77b0c075b1a48135d8fb1c
parentMerge pull request #6569 (diff)
parentByteSlice: Fix persisting ptr to std::moved SSO buffer (diff)
downloadmonero-4f6dbcd00e3c4b3edfefe4ad5c64172d37b5a011.tar.xz
Merge pull request #6572
438b846 ByteSlice: Fix persisting ptr to std::moved SSO buffer (Doy-lee)
-rw-r--r--contrib/epee/src/byte_slice.cpp5
-rw-r--r--tests/unit_tests/epee_utils.cpp23
2 files changed, 27 insertions, 1 deletions
diff --git a/contrib/epee/src/byte_slice.cpp b/contrib/epee/src/byte_slice.cpp
index 12cc83e6c..faf7689be 100644
--- a/contrib/epee/src/byte_slice.cpp
+++ b/contrib/epee/src/byte_slice.cpp
@@ -133,10 +133,13 @@ namespace epee
template<typename T>
byte_slice::byte_slice(const adapt_buffer, T&& buffer)
- : storage_(nullptr), portion_(to_byte_span(to_span(buffer)))
+ : storage_(nullptr), portion_(nullptr)
{
if (!buffer.empty())
+ {
storage_ = allocate_slice<adapted_byte_slice<T>>(0, std::move(buffer));
+ portion_ = to_byte_span(to_span(static_cast<adapted_byte_slice<T> *>(storage_.get())->buffer));
+ }
}
byte_slice::byte_slice(std::initializer_list<span<const std::uint8_t>> sources)
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index a2cec965e..0f91671a7 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -387,6 +387,29 @@ TEST(ByteSlice, Construction)
EXPECT_FALSE(std::is_copy_assignable<epee::byte_slice>());
}
+TEST(ByteSlice, DataReturnedMatches)
+{
+ for (int i = 64; i > 0; i--)
+ {
+ std::string sso_string(i, 'a');
+ std::string original = sso_string;
+ epee::byte_slice slice{std::move(sso_string)};
+
+ EXPECT_EQ(slice.size(), original.size());
+ EXPECT_EQ(memcmp(slice.data(), original.data(), original.size()), 0);
+ }
+
+ for (int i = 64; i > 0; i--)
+ {
+ std::vector<uint8_t> sso_vector(i, 'a');
+ std::vector<uint8_t> original = sso_vector;
+ epee::byte_slice slice{std::move(sso_vector)};
+
+ EXPECT_EQ(slice.size(), original.size());
+ EXPECT_EQ(memcmp(slice.data(), original.data(), original.size()), 0);
+ }
+}
+
TEST(ByteSlice, NoExcept)
{
EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_slice>());