mirror of
https://github.com/monero-project/monero.git
synced 2024-10-01 11:49:47 -04:00
Merge pull request #6559
15538f7
ByteSlice: Fix persisting ptr to std::moved SSO buffer (Doy-lee)
This commit is contained in:
commit
35e2520115
@ -133,10 +133,13 @@ namespace epee
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
byte_slice::byte_slice(const adapt_buffer, T&& buffer)
|
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())
|
if (!buffer.empty())
|
||||||
|
{
|
||||||
storage_ = allocate_slice<adapted_byte_slice<T>>(0, std::move(buffer));
|
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)
|
byte_slice::byte_slice(std::initializer_list<span<const std::uint8_t>> sources)
|
||||||
|
@ -387,6 +387,29 @@ TEST(ByteSlice, Construction)
|
|||||||
EXPECT_FALSE(std::is_copy_assignable<epee::byte_slice>());
|
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)
|
TEST(ByteSlice, NoExcept)
|
||||||
{
|
{
|
||||||
EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_slice>());
|
EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_slice>());
|
||||||
|
Loading…
Reference in New Issue
Block a user