From 81fd09775ccad41fa114d6125a4badb0abfbabeb Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Sat, 5 May 2018 23:10:01 +0200 Subject: [PATCH] Fix compilation with GCC 8 --- include/libtorrent/lazy_entry.hpp | 9 ++++++--- include/libtorrent/netlink.hpp | 2 +- src/lazy_bdecode.cpp | 19 +++++++++++++++---- src/torrent_info.cpp | 1 - test/test_bencoding.cpp | 10 ++++++++-- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index 37397e30d..78859a5cf 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -354,6 +354,9 @@ namespace libtorrent { swap(m_len, e.m_len); } + lazy_entry(lazy_entry&&); + lazy_entry& operator=(lazy_entry&&); + private: int capacity() const; @@ -382,13 +385,13 @@ namespace libtorrent { std::uint32_t m_type:3; // non-copyable - lazy_entry(lazy_entry const&); - lazy_entry const& operator=(lazy_entry const&); + lazy_entry(lazy_entry const&) = delete; + lazy_entry const& operator=(lazy_entry const&) = delete; }; struct TORRENT_DEPRECATED lazy_dict_entry { - char const* name; + char const* name = nullptr; lazy_entry val; }; diff --git a/include/libtorrent/netlink.hpp b/include/libtorrent/netlink.hpp index 32280b193..87e9d02c5 100644 --- a/include/libtorrent/netlink.hpp +++ b/include/libtorrent/netlink.hpp @@ -88,7 +88,7 @@ namespace libtorrent { data_type* data() { - return &m_sockaddr; + return reinterpret_cast(&m_sockaddr); } const data_type* data() const diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index 6f1aebbdd..75a80ef7f 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/lazy_entry.hpp" #include "libtorrent/bdecode.hpp" // for error codes #include "libtorrent/string_util.hpp" // for is_digit +#include #include // for memset #include // for numeric_limits #include // for snprintf @@ -266,8 +267,7 @@ namespace { int const capacity = this->capacity() * lazy_entry_grow_factor / 100; auto* tmp = new (std::nothrow) lazy_dict_entry[capacity + 1]; if (tmp == nullptr) return nullptr; - std::memcpy(tmp, m_data.dict, sizeof(lazy_dict_entry) * (m_size + 1)); - for (int i = 0; i < int(m_size); ++i) m_data.dict[i + 1].val.release(); + std::move(m_data.dict, m_data.dict + m_size + 1, tmp); delete[] m_data.dict; m_data.dict = tmp; @@ -437,8 +437,7 @@ namespace { int const capacity = this->capacity() * lazy_entry_grow_factor / 100; lazy_entry* tmp = new (std::nothrow) lazy_entry[capacity + 1]; if (tmp == nullptr) return nullptr; - std::memcpy(tmp, m_data.list, sizeof(lazy_entry) * (m_size + 1)); - for (int i = 0; i < int(m_size); ++i) m_data.list[i + 1].release(); + std::move(m_data.list, m_data.list + m_size + 1, tmp); delete[] m_data.list; m_data.list = tmp; @@ -492,6 +491,18 @@ namespace { return {m_begin, m_len}; } + lazy_entry::lazy_entry(lazy_entry&& other) + : lazy_entry() + { + this->swap(other); + } + + lazy_entry& lazy_entry::operator=(lazy_entry&& other) + { + this->swap(other); + return *this; + } + namespace { int line_longer_than(lazy_entry const& e, int limit) diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 6c3312372..cd68bc192 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -1094,7 +1094,6 @@ namespace { int const num_nodes = merkle_num_nodes(num_leafs); m_merkle_first_leaf = num_nodes - num_leafs; m_merkle_tree.resize(num_nodes); - std::memset(m_merkle_tree.data(), 0, aux::numeric_cast(num_nodes * 20)); m_merkle_tree[0].assign(root_hash.string_ptr()); } diff --git a/test/test_bencoding.cpp b/test/test_bencoding.cpp index 3b4a5f534..b82f9f721 100644 --- a/test/test_bencoding.cpp +++ b/test/test_bencoding.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include #ifndef TORRENT_NO_DEPRECATE #include "libtorrent/lazy_entry.hpp" @@ -629,12 +630,17 @@ TORRENT_TEST(lazy_entry) for (int i = 0; i < int(sizeof(b)/sizeof(b[0])); ++i) { - lazy_entry e; + lazy_entry tmp; error_code ec; - int ret = lazy_bdecode(b[i], b[i] + strlen(b[i]), e, ec, nullptr); + int ret = lazy_bdecode(b[i], b[i] + strlen(b[i]), tmp, ec, nullptr); + lazy_entry e; + e = std::move(tmp); TEST_EQUAL(ret, -1); TEST_CHECK(ec == error_code(bdecode_errors::unexpected_eof)); std::printf("%s\n", print_entry(e).c_str()); + + lazy_entry* moved = new lazy_entry(std::move(e)); + delete moved; } } }