diff options
| author | Doyle <doylet@protonmail.com> | 2020-05-19 18:45:32 +1000 |
|---|---|---|
| committer | Doyle <doylet@protonmail.com> | 2020-05-20 10:13:58 +1000 |
| commit | 15538f7e3f8fdafb13b99554a8726850caeef937 (patch) | |
| tree | d53316319bcdadd33678962c64295e7720a275f3 /contrib/epee/src/byte_slice.cpp | |
| parent | dbba6004f57e4aa10c0c989ed403b242592e1e5b (diff) | |
| download | monzero-core-15538f7e3f8fdafb13b99554a8726850caeef937.tar.gz monzero-core-15538f7e3f8fdafb13b99554a8726850caeef937.tar.xz monzero-core-15538f7e3f8fdafb13b99554a8726850caeef937.zip | |
ByteSlice: Fix persisting ptr to std::moved SSO buffer
The Bug:
1. Construct `byte_slice.portion_` with `epee::span(buffer)` which copies a pointer to the SSO buffer to `byte_slice.portion_`
2. It constructs `byte_slice.storage_` with `std::move(buffer)` (normally this swap pointers, but SSO means a memcpy and clear on the original SSO buffer)
3. `slice.data()` returns a pointer from `slice.portion_` that points to the original SSO cleared buffer, `slice.storage_` has the actual string.
Diffstat (limited to 'contrib/epee/src/byte_slice.cpp')
| -rw-r--r-- | contrib/epee/src/byte_slice.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/contrib/epee/src/byte_slice.cpp b/contrib/epee/src/byte_slice.cpp index 99c37fae3..25fec4452 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) |
