From a05f0ba8a499608420327769b0f17c997d4fd0e1 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Sun, 11 Dec 2016 20:24:26 -0500 Subject: [PATCH] refactor for more modern struct initialization and use of api --- src/assert.cpp | 3 +-- src/bdecode.cpp | 30 ++++++++++++------------------ src/choker.cpp | 2 +- src/disk_io_thread.cpp | 2 +- src/enum_net.cpp | 17 ++++++----------- src/file_storage.cpp | 2 ++ src/session_impl.cpp | 2 +- src/udp_tracker_connection.cpp | 4 +--- src/web_peer_connection.cpp | 8 ++++---- 9 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/assert.cpp b/src/assert.cpp index eaa340d64..0214f6a8e 100644 --- a/src/assert.cpp +++ b/src/assert.cpp @@ -174,8 +174,7 @@ TORRENT_EXPORT void print_backtrace(char* out, int len, int max_depth int size = 0; std::array stack; - STACKFRAME64 stack_frame; - memset(&stack_frame, 0, sizeof(stack_frame)); + STACKFRAME64 stack_frame = {}; #if defined(_WIN64) int const machine_type = IMAGE_FILE_MACHINE_AMD64; stack_frame.AddrPC.Offset = context_record.Rip; diff --git a/src/bdecode.cpp b/src/bdecode.cpp index 5278a4043..6b6bb2ecf 100644 --- a/src/bdecode.cpp +++ b/src/bdecode.cpp @@ -697,8 +697,7 @@ namespace libtorrent // we push it into the stack so that we know where to fill // in the next_node field once we pop this node off the stack. // i.e. get to the node following the dictionary in the buffer - ret.m_tokens.push_back(bdecode_token(start - orig_start - , bdecode_token::dict)); + ret.m_tokens.push_back({start - orig_start, bdecode_token::dict}); ++start; break; case 'l': @@ -706,8 +705,7 @@ namespace libtorrent // we push it into the stack so that we know where to fill // in the next_node field once we pop this node off the stack. // i.e. get to the node following the list in the buffer - ret.m_tokens.push_back(bdecode_token(start - orig_start - , bdecode_token::list)); + ret.m_tokens.push_back({start - orig_start, bdecode_token::list}); ++start; break; case 'i': @@ -725,8 +723,8 @@ namespace libtorrent start = int_start; TORRENT_FAIL_BDECODE(e); } - ret.m_tokens.push_back(bdecode_token(int_start - orig_start - , 1, bdecode_token::integer, 1)); + ret.m_tokens.push_back({int_start - orig_start + , 1, bdecode_token::integer, 1}); TORRENT_ASSERT(*start == 'e'); // skip 'e' @@ -749,8 +747,7 @@ namespace libtorrent } // insert the end-of-sequence token - ret.m_tokens.push_back(bdecode_token(start - orig_start, 1 - , bdecode_token::end)); + ret.m_tokens.push_back({start - orig_start, 1, bdecode_token::end}); // and back-patch the start of this sequence with the offset // to the next token we'll insert @@ -798,8 +795,8 @@ namespace libtorrent if (start - str_start - 2 > detail::bdecode_token::max_header) TORRENT_FAIL_BDECODE(bdecode_errors::limit_exceeded); - ret.m_tokens.push_back(bdecode_token(str_start - orig_start - , 1, bdecode_token::string, std::uint8_t(start - str_start))); + ret.m_tokens.push_back({str_start - orig_start + , 1, bdecode_token::string, std::uint8_t(start - str_start)}); start += len; break; } @@ -831,25 +828,22 @@ done: && stack[sp].state == 1) { // insert an empty dictionary as the value - ret.m_tokens.push_back(bdecode_token(start - orig_start - , 2, bdecode_token::dict)); - ret.m_tokens.push_back(bdecode_token(start - orig_start - , bdecode_token::end)); + ret.m_tokens.push_back({start - orig_start, 2, bdecode_token::dict}); + ret.m_tokens.push_back({start - orig_start, bdecode_token::end}); } int const top = stack[sp].token; TORRENT_ASSERT(ret.m_tokens.size() - top <= bdecode_token::max_next_item); ret.m_tokens[top].next_item = std::uint32_t(ret.m_tokens.size() - top); - ret.m_tokens.push_back(bdecode_token(start - orig_start, 1, bdecode_token::end)); + ret.m_tokens.push_back({start - orig_start, 1, bdecode_token::end}); } - ret.m_tokens.push_back(bdecode_token(start - orig_start, 0 - , bdecode_token::end)); + ret.m_tokens.push_back({start - orig_start, 0, bdecode_token::end}); ret.m_token_idx = 0; ret.m_buffer = orig_start; ret.m_buffer_size = int(start - orig_start); - ret.m_root_tokens = &ret.m_tokens[0]; + ret.m_root_tokens = ret.m_tokens.data(); return ec ? -1 : 0; } diff --git a/src/choker.cpp b/src/choker.cpp index 71714a82b..ee08e10d9 100644 --- a/src/choker.cpp +++ b/src/choker.cpp @@ -319,7 +319,7 @@ namespace libtorrent for (auto const p : peers) { - TORRENT_ASSERT(p); + TORRENT_ASSERT(p != nullptr); if (p->est_reciprocation_rate() > upload_capacity_left) break; diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index e45ff5180..3624f2322 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1252,7 +1252,7 @@ namespace libtorrent std::unique_lock l2(m_cache_mutex); pe = m_disk_cache.find_piece(j); - if (pe) maybe_issue_queued_read_jobs(pe, completed_jobs); + if (pe != nullptr) maybe_issue_queued_read_jobs(pe, completed_jobs); return s; } diff --git a/src/enum_net.cpp b/src/enum_net.cpp index 4f7d293bc..4e881f153 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -232,8 +232,7 @@ namespace libtorrent { namespace #endif if_indextoname(if_index, rt_info->name); - ifreq req; - memset(&req, 0, sizeof(req)); + ifreq req = {}; if_indextoname(if_index, req.ifr_name); ioctl(s, siocgifmtu, &req); rt_info->mtu = req.ifr_mtu; @@ -290,8 +289,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl if_indextoname(rtm->rtm_index, rt_info->name); // TODO: get the MTU (and other interesting metrics) from the rt_msghdr instead - ifreq req; - memset(&req, 0, sizeof(req)); + ifreq req = {}; if_indextoname(rtm->rtm_index, req.ifr_name); // ignore errors here. This is best-effort @@ -478,8 +476,7 @@ namespace libtorrent ip_interface iface; if (iface_from_ifaddrs(ifa, iface)) { - ifreq req; - std::memset(&req, 0, sizeof(req)); + ifreq req = {}; // -1 to leave a 0-terminator std::strncpy(req.ifr_name, iface.name, IF_NAMESIZE - 1); @@ -539,8 +536,7 @@ namespace libtorrent iface.interface_address = sockaddr_to_address(&item.ifr_addr); strcpy(iface.name, item.ifr_name); - ifreq req; - memset(&req, 0, sizeof(req)); + ifreq req = {}; // -1 to leave a 0-terminator strncpy(req.ifr_name, item.ifr_name, IF_NAMESIZE - 1); if (ioctl(s, siocgifmtu, &req) < 0) @@ -555,7 +551,7 @@ namespace libtorrent iface.mtu = req.ifr_metric; // according to tcp/ip reference #endif - memset(&req, 0, sizeof(req)); + std::memset(&req, 0, sizeof(req)); strncpy(req.ifr_name, item.ifr_name, IF_NAMESIZE - 1); if (ioctl(s, SIOCGIFNETMASK, &req) < 0) { @@ -1093,8 +1089,7 @@ namespace libtorrent int seq = 0; - char msg[BUFSIZE]; - memset(msg, 0, BUFSIZE); + char msg[BUFSIZE] = {}; nlmsghdr* nl_msg = reinterpret_cast(msg); nl_msg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg)); diff --git a/src/file_storage.cpp b/src/file_storage.cpp index 33b012105..321a20204 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -819,6 +819,7 @@ namespace libtorrent int file_storage::file_flags(int index) const { + TORRENT_ASSERT_PRECOND(index >= 0 && index < int(m_files.size())); internal_file_entry const& fe = m_files[index]; return (fe.pad_file ? flag_pad_file : 0) | (fe.hidden_attribute ? flag_hidden : 0) @@ -828,6 +829,7 @@ namespace libtorrent bool file_storage::file_absolute_path(int index) const { + TORRENT_ASSERT_PRECOND(index >= 0 && index < int(m_files.size())); internal_file_entry const& fe = m_files[index]; return fe.path_index == -2; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 09ba2b862..d9094000d 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4175,7 +4175,7 @@ namespace aux { // all the other ones. for (auto p : peers) { - TORRENT_ASSERT(p); + TORRENT_ASSERT(p != nullptr); TORRENT_ASSERT(!p->ignore_unchoke_slots()); // this will update the m_uploaded_at_last_unchoke diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index e1183cca1..7670aab0c 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -454,10 +454,8 @@ namespace libtorrent void udp_tracker_connection::update_transaction_id() { - std::uint32_t new_tid; - // don't use 0, because that has special meaning (unintialized) - new_tid = random(0xfffffffe) + 1; + std::uint32_t const new_tid = random(0xfffffffe) + 1; if (m_transaction_id != 0) m_man.update_transaction_id(shared_from_this(), new_tid); diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 95e479ffc..a22ec167b 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -650,7 +650,7 @@ void web_peer_connection::handle_redirect(int const bytes_left) if (web->have_files.get_bit(file_index) == false) { web->have_files.set_bit(file_index); - if (web->peer_info.connection) + if (web->peer_info.connection != nullptr) { peer_connection* pc = static_cast(web->peer_info.connection); @@ -1029,7 +1029,7 @@ void web_peer_connection::incoming_payload(char const* buf, int len) // to not exceed the size of the next bittorrent request to be delivered. // m_piece can only hold the response for a single BT request at a time m_piece.resize(piece_size + copy_size); - std::memcpy(&m_piece[0] + piece_size, buf, copy_size); + std::memcpy(m_piece.data() + piece_size, buf, copy_size); len -= copy_size; buf += copy_size; @@ -1055,7 +1055,7 @@ void web_peer_connection::incoming_payload(char const* buf, int len) peer_request const front_request_copy = front_request; m_requests.pop_front(); - incoming_piece(front_request_copy, &m_piece[0]); + incoming_piece(front_request_copy, m_piece.data()); m_piece.clear(); } @@ -1108,7 +1108,7 @@ void web_peer_connection::maybe_harvest_piece() #endif m_requests.pop_front(); - incoming_piece(front_request, &m_piece[0]); + incoming_piece(front_request, m_piece.data()); m_piece.clear(); }