From b797b4c94f1c8ff9982bb4f8ba7ae6c4dc001f7c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 26 Oct 2021 13:02:07 -0500 Subject: [PATCH] refactor: cppcoreguidelines-init-variables pt. 13 (#2043) * refactor: fix uninit var warnings in ptrarray * refactor: fix uninit var warnings in bitfield * refactor: fix uninit var warnings in handshake * refactor: fix uninit var warnings in tr-dht * refactor: fix uninit var warnings in natpmp Co-authored-by: Mike Gelfand --- libtransmission/bitfield.cc | 6 ++--- libtransmission/handshake.cc | 44 ++++++++++++------------------------ libtransmission/natpmp.cc | 20 ++++------------ libtransmission/ptrarray.cc | 11 ++++----- libtransmission/tr-dht.cc | 44 ++++++++++-------------------------- 5 files changed, 37 insertions(+), 88 deletions(-) diff --git a/libtransmission/bitfield.cc b/libtransmission/bitfield.cc index 8c7d97238..8e13341e7 100644 --- a/libtransmission/bitfield.cc +++ b/libtransmission/bitfield.cc @@ -88,10 +88,9 @@ size_t tr_bitfield::countFlags(size_t begin, size_t end) const if (first_byte == last_byte) { - int i; uint8_t val = flags_[first_byte]; - i = begin - (first_byte * 8); + int i = begin - (first_byte * 8); val <<= i; val >>= i; i = (last_byte + 1) * 8 - end; @@ -102,12 +101,11 @@ size_t tr_bitfield::countFlags(size_t begin, size_t end) const } else { - uint8_t val; size_t const walk_end = std::min(std::size(flags_), last_byte); /* first byte */ size_t const first_shift = begin - (first_byte * 8); - val = flags_[first_byte]; + uint8_t val = flags_[first_byte]; val <<= first_shift; val >>= first_shift; ret += trueBitCount[val]; diff --git a/libtransmission/handshake.cc b/libtransmission/handshake.cc index 0ec2f7e21..52679fd33 100644 --- a/libtransmission/handshake.cc +++ b/libtransmission/handshake.cc @@ -288,12 +288,11 @@ static handshake_parse_err_t parseHandshake(tr_handshake* handshake, struct evbu static void sendYa(tr_handshake* handshake) { int len; - uint8_t const* public_key; char outbuf[KEY_LEN + PadA_MAXLEN]; char* walk = outbuf; /* add our public key (Ya) */ - public_key = tr_cryptoGetMyPublicKey(handshake->crypto, &len); + uint8_t const* const public_key = tr_cryptoGetMyPublicKey(handshake->crypto, &len); TR_ASSERT(len == KEY_LEN); TR_ASSERT(public_key != nullptr); memcpy(walk, public_key, len); @@ -368,9 +367,7 @@ static void computeRequestHash(tr_handshake const* handshake, char const* name, static ReadState readYb(tr_handshake* handshake, struct evbuffer* inbuf) { - bool isEncrypted; uint8_t yb[KEY_LEN]; - struct evbuffer* outbuf; size_t needlen = HANDSHAKE_NAME_LEN; if (evbuffer_get_length(inbuf) < needlen) @@ -378,7 +375,7 @@ static ReadState readYb(tr_handshake* handshake, struct evbuffer* inbuf) return READ_LATER; } - isEncrypted = memcmp(evbuffer_pullup(inbuf, HANDSHAKE_NAME_LEN), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN) != 0; + bool const isEncrypted = memcmp(evbuffer_pullup(inbuf, HANDSHAKE_NAME_LEN), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN) != 0; if (isEncrypted) { @@ -412,7 +409,7 @@ static ReadState readYb(tr_handshake* handshake, struct evbuffer* inbuf) /* now send these: HASH('req1', S), HASH('req2', SKEY) xor HASH('req3', S), * ENCRYPT(VC, crypto_provide, len(PadC), PadC, len(IA)), ENCRYPT(IA) */ - outbuf = evbuffer_new(); + evbuffer* const outbuf = evbuffer_new(); /* HASH('req1', S) */ { @@ -718,7 +715,6 @@ static ReadState readYa(tr_handshake* handshake, struct evbuffer* inbuf) uint8_t ya[KEY_LEN]; uint8_t* walk; uint8_t outbuf[KEY_LEN + PadB_MAXLEN]; - uint8_t const* myKey; int len; dbgmsg(handshake, "in readYa... need %d, have %zu", KEY_LEN, evbuffer_get_length(inbuf)); @@ -741,7 +737,7 @@ static ReadState readYa(tr_handshake* handshake, struct evbuffer* inbuf) /* send our public key to the peer */ dbgmsg(handshake, "sending B->A: Diffie Hellman Yb, PadB"); walk = outbuf; - myKey = tr_cryptoGetMyPublicKey(handshake->crypto, &len); + uint8_t const* const myKey = tr_cryptoGetMyPublicKey(handshake->crypto, &len); memcpy(walk, myKey, len); walk += len; len = tr_rand_int(PadB_MAXLEN); @@ -809,8 +805,8 @@ static ReadState readCryptoProvide(tr_handshake* handshake, struct evbuffer* inb obfuscatedTorrentHash[i] = req2[i] ^ req3[i]; } - tr_torrent const* tor; - if ((tor = tr_torrentFindFromObfuscatedHash(handshake->session, obfuscatedTorrentHash)) != nullptr) + tr_torrent const* const tor = tr_torrentFindFromObfuscatedHash(handshake->session, obfuscatedTorrentHash); + if (tor != nullptr) { bool const clientIsSeed = tr_torrentIsSeed(tor); bool const peerIsSeed = tr_peerMgrPeerIsSeed(tor, tr_peerIoGetAddress(handshake->io, nullptr)); @@ -848,7 +844,6 @@ static ReadState readCryptoProvide(tr_handshake* handshake, struct evbuffer* inb static ReadState readPadC(tr_handshake* handshake, struct evbuffer* inbuf) { - char* padc; uint16_t ia_len; size_t const needlen = handshake->pad_c_len + sizeof(uint16_t); @@ -858,7 +853,7 @@ static ReadState readPadC(tr_handshake* handshake, struct evbuffer* inbuf) } /* read the throwaway padc */ - padc = tr_new(char, handshake->pad_c_len); + char* const padc = tr_new(char, handshake->pad_c_len); tr_peerIoReadBytes(handshake->io, inbuf, padc, handshake->pad_c_len); tr_free(padc); @@ -873,8 +868,6 @@ static ReadState readPadC(tr_handshake* handshake, struct evbuffer* inbuf) static ReadState readIA(tr_handshake* handshake, struct evbuffer const* inbuf) { size_t const needlen = handshake->ia_len; - struct evbuffer* outbuf; - uint32_t crypto_select; dbgmsg(handshake, "reading IA... have %zu, need %zu", evbuffer_get_length(inbuf), needlen); @@ -888,7 +881,7 @@ static ReadState readIA(tr_handshake* handshake, struct evbuffer const* inbuf) **/ tr_cryptoEncryptInit(handshake->crypto); - outbuf = evbuffer_new(); + evbuffer* const outbuf = evbuffer_new(); { /* send VC */ @@ -899,7 +892,7 @@ static ReadState readIA(tr_handshake* handshake, struct evbuffer const* inbuf) } /* send crypto_select */ - crypto_select = getCryptoSelect(handshake, handshake->crypto_provide); + uint32_t const crypto_select = getCryptoSelect(handshake, handshake->crypto_provide); if (crypto_select != 0) { @@ -956,7 +949,6 @@ static ReadState readIA(tr_handshake* handshake, struct evbuffer const* inbuf) static ReadState readPayloadStream(tr_handshake* handshake, struct evbuffer* inbuf) { - handshake_parse_err_t i; size_t const needlen = HANDSHAKE_SIZE; dbgmsg(handshake, "reading payload stream... have %zu, need %zu", evbuffer_get_length(inbuf), needlen); @@ -967,7 +959,7 @@ static ReadState readPayloadStream(tr_handshake* handshake, struct evbuffer* inb } /* parse the handshake ... */ - i = parseHandshake(handshake, inbuf); + handshake_parse_err_t const i = parseHandshake(handshake, inbuf); dbgmsg(handshake, "parseHandshake returned %d", i); if (i != HANDSHAKE_OK) @@ -1133,16 +1125,9 @@ static void gotError(tr_peerIo* io, short what, void* vhandshake) { /* This peer probably doesn't speak uTP. */ - tr_torrent* tor; - - if (tr_peerIoHasTorrentHash(io)) - { - tor = tr_torrentFindFromHash(handshake->session, tr_peerIoGetTorrentHash(io)); - } - else - { - tor = nullptr; - } + tr_torrent* const tor = tr_peerIoHasTorrentHash(io) ? + tr_torrentFindFromHash(handshake->session, tr_peerIoGetTorrentHash(io)) : + nullptr; /* Don't mark a peer as non-uTP unless it's really a connect failure. */ if ((errcode == ETIMEDOUT || errcode == ECONNREFUSED) && tr_isTorrent(tor)) @@ -1196,10 +1181,9 @@ tr_handshake* tr_handshakeNew( tr_handshake_done_func done_func, void* done_func_user_data) { - tr_handshake* handshake; tr_session* session = tr_peerIoGetSession(io); - handshake = tr_new0(tr_handshake, 1); + auto* const handshake = tr_new0(tr_handshake, 1); handshake->io = io; handshake->crypto = tr_peerIoGetCrypto(io); handshake->encryptionMode = encryptionMode; diff --git a/libtransmission/natpmp.cc b/libtransmission/natpmp.cc index 0fd6ad5c1..aaa8c55ef 100644 --- a/libtransmission/natpmp.cc +++ b/libtransmission/natpmp.cc @@ -86,9 +86,7 @@ static void logVal(char const* func, int ret) struct tr_natpmp* tr_natpmpInit(void) { - struct tr_natpmp* nat; - - nat = tr_new0(struct tr_natpmp, 1); + auto* const nat = tr_new0(struct tr_natpmp, 1); nat->state = TR_NATPMP_DISCOVER; nat->public_port = 0; nat->private_port = 0; @@ -117,8 +115,6 @@ static void setCommandTime(struct tr_natpmp* nat) tr_port_forwarding tr_natpmpPulse(struct tr_natpmp* nat, tr_port private_port, bool is_enabled, tr_port* public_port) { - tr_port_forwarding ret; - if (is_enabled && nat->state == TR_NATPMP_DISCOVER) { int val = initnatpmp(&nat->natpmp, 0, 0); @@ -237,24 +233,18 @@ tr_port_forwarding tr_natpmpPulse(struct tr_natpmp* nat, tr_port private_port, b return nat->is_mapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED; case TR_NATPMP_DISCOVER: - ret = TR_PORT_UNMAPPED; - break; + return TR_PORT_UNMAPPED; case TR_NATPMP_RECV_PUB: case TR_NATPMP_SEND_MAP: case TR_NATPMP_RECV_MAP: - ret = TR_PORT_MAPPING; - break; + return TR_PORT_MAPPING; case TR_NATPMP_SEND_UNMAP: case TR_NATPMP_RECV_UNMAP: - ret = TR_PORT_UNMAPPING; - break; + return TR_PORT_UNMAPPING; default: - ret = TR_PORT_ERROR; - break; + return TR_PORT_ERROR; } - - return ret; } diff --git a/libtransmission/ptrarray.cc b/libtransmission/ptrarray.cc index 02696cee9..844746cd3 100644 --- a/libtransmission/ptrarray.cc +++ b/libtransmission/ptrarray.cc @@ -200,12 +200,10 @@ static void assertIndexIsSortedAndUnique(tr_ptrArray const* t, int pos, tr_voidp int tr_ptrArrayInsertSorted(tr_ptrArray* t, void* ptr, tr_voidptr_compare_func compare) { - int pos; - int ret; assertArrayIsSortedAndUnique(t, compare); - pos = tr_ptrArrayLowerBound(t, ptr, compare, nullptr); - ret = tr_ptrArrayInsert(t, ptr, pos); + int const pos = tr_ptrArrayLowerBound(t, ptr, compare, nullptr); + int const ret = tr_ptrArrayInsert(t, ptr, pos); assertIndexIsSortedAndUnique(t, ret, compare); return ret; @@ -220,13 +218,12 @@ void* tr_ptrArrayFindSorted(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_ static void* tr_ptrArrayRemoveSortedValue(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare) { - int pos; - bool match; void* ret = nullptr; assertArrayIsSortedAndUnique(t, compare); - pos = tr_ptrArrayLowerBound(t, ptr, compare, &match); + bool match = false; + int const pos = tr_ptrArrayLowerBound(t, ptr, compare, &match); if (match) { diff --git a/libtransmission/tr-dht.cc b/libtransmission/tr-dht.cc index 4c25a3672..12164bd30 100644 --- a/libtransmission/tr-dht.cc +++ b/libtransmission/tr-dht.cc @@ -79,14 +79,12 @@ struct bootstrap_closure static bool bootstrap_done(tr_session* session, int af) { - int status; - if (af == 0) { return bootstrap_done(session, AF_INET) && bootstrap_done(session, AF_INET6); } - status = tr_dhtStatus(session, af, nullptr); + int const status = tr_dhtStatus(session, af, nullptr); return status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED; } @@ -116,9 +114,7 @@ static void bootstrap_from_name(char const* name, tr_port port, int af) { struct addrinfo hints; struct addrinfo* info; - struct addrinfo* infop; char pp[10]; - int rc; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; @@ -126,7 +122,7 @@ static void bootstrap_from_name(char const* name, tr_port port, int af) /* No, just passing p + 1 to gai won't work. */ tr_snprintf(pp, sizeof(pp), "%d", (int)port); - rc = getaddrinfo(name, pp, &hints, &info); + int const rc = getaddrinfo(name, pp, &hints, &info); if (rc != 0) { @@ -134,8 +130,7 @@ static void bootstrap_from_name(char const* name, tr_port port, int af) return; } - infop = info; - + addrinfo* infop = info; while (infop != nullptr) { dht_ping_node(infop->ai_addr, infop->ai_addrlen); @@ -222,10 +217,9 @@ static void dht_bootstrap(void* closure) if (!bootstrap_done(cl->session, 0)) { - char* bootstrap_file; tr_sys_file_t f = TR_BAD_SYS_FILE; - bootstrap_file = tr_buildPath(cl->session->configDir, "dht.bootstrap", nullptr); + char* const bootstrap_file = tr_buildPath(cl->session->configDir, "dht.bootstrap", nullptr); if (bootstrap_file != nullptr) { @@ -315,9 +309,7 @@ static void dht_bootstrap(void* closure) int tr_dhtInit(tr_session* ss) { tr_variant benc; - int rc; bool have_id = false; - char* dat_file; uint8_t* nodes = nullptr; uint8_t* nodes6 = nullptr; uint8_t const* raw; @@ -337,8 +329,8 @@ int tr_dhtInit(tr_session* ss) dht_debug = stderr; } - dat_file = tr_buildPath(ss->configDir, "dht.dat", nullptr); - rc = tr_variantFromFile(&benc, TR_VARIANT_FMT_BENC, dat_file, nullptr) ? 0 : -1; + char* const dat_file = tr_buildPath(ss->configDir, "dht.dat", nullptr); + int rc = tr_variantFromFile(&benc, TR_VARIANT_FMT_BENC, dat_file, nullptr) ? 0 : -1; tr_free(dat_file); if (rc == 0) @@ -647,23 +639,14 @@ static void callback(void* /*ignore*/, int event, unsigned char const* info_hash { if (event == DHT_EVENT_VALUES || event == DHT_EVENT_VALUES6) { - tr_torrent* tor; tr_sessionLock(session_); - tor = tr_torrentFindFromHash(session_, info_hash); + tr_torrent* const tor = tr_torrentFindFromHash(session_, info_hash); if (tor != nullptr && tr_torrentAllowsDHT(tor)) { - size_t n; - tr_pex* pex; - - if (event == DHT_EVENT_VALUES) - { - pex = tr_peerMgrCompactToPex(data, data_len, nullptr, 0, &n); - } - else - { - pex = tr_peerMgrCompact6ToPex(data, data_len, nullptr, 0, &n); - } + size_t n = 0; + tr_pex* const pex = event == DHT_EVENT_VALUES ? tr_peerMgrCompactToPex(data, data_len, nullptr, 0, &n) : + tr_peerMgrCompact6ToPex(data, data_len, nullptr, 0, &n); tr_peerMgrAddPex(tor, TR_PEER_FROM_DHT, pex, n); @@ -695,8 +678,6 @@ static void callback(void* /*ignore*/, int event, unsigned char const* info_hash static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce) { - int rc; - int status; int numnodes; int ret = 0; @@ -705,7 +686,7 @@ static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce) return -1; } - status = tr_dhtStatus(tor->session, af, &numnodes); + int const status = tr_dhtStatus(tor->session, af, &numnodes); if (status == TR_DHT_STOPPED) { @@ -715,8 +696,7 @@ static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce) if (status >= TR_DHT_POOR) { - rc = dht_search(tor->info.hash, announce ? tr_sessionGetPeerPort(session_) : 0, af, callback, nullptr); - + int const rc = dht_search(tor->info.hash, announce ? tr_sessionGetPeerPort(session_) : 0, af, callback, nullptr); if (rc >= 0) { tr_logAddTorInfo(