1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00

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 <mikedld@users.noreply.github.com>
This commit is contained in:
Charles Kerr 2021-10-26 13:02:07 -05:00 committed by GitHub
parent c42e05b42f
commit b797b4c94f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 88 deletions

View file

@ -88,10 +88,9 @@ size_t tr_bitfield::countFlags(size_t begin, size_t end) const
if (first_byte == last_byte) if (first_byte == last_byte)
{ {
int i;
uint8_t val = flags_[first_byte]; uint8_t val = flags_[first_byte];
i = begin - (first_byte * 8); int i = begin - (first_byte * 8);
val <<= i; val <<= i;
val >>= i; val >>= i;
i = (last_byte + 1) * 8 - end; i = (last_byte + 1) * 8 - end;
@ -102,12 +101,11 @@ size_t tr_bitfield::countFlags(size_t begin, size_t end) const
} }
else else
{ {
uint8_t val;
size_t const walk_end = std::min(std::size(flags_), last_byte); size_t const walk_end = std::min(std::size(flags_), last_byte);
/* first byte */ /* first byte */
size_t const first_shift = begin - (first_byte * 8); 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;
val >>= first_shift; val >>= first_shift;
ret += trueBitCount[val]; ret += trueBitCount[val];

View file

@ -288,12 +288,11 @@ static handshake_parse_err_t parseHandshake(tr_handshake* handshake, struct evbu
static void sendYa(tr_handshake* handshake) static void sendYa(tr_handshake* handshake)
{ {
int len; int len;
uint8_t const* public_key;
char outbuf[KEY_LEN + PadA_MAXLEN]; char outbuf[KEY_LEN + PadA_MAXLEN];
char* walk = outbuf; char* walk = outbuf;
/* add our public key (Ya) */ /* 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(len == KEY_LEN);
TR_ASSERT(public_key != nullptr); TR_ASSERT(public_key != nullptr);
memcpy(walk, public_key, len); 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) static ReadState readYb(tr_handshake* handshake, struct evbuffer* inbuf)
{ {
bool isEncrypted;
uint8_t yb[KEY_LEN]; uint8_t yb[KEY_LEN];
struct evbuffer* outbuf;
size_t needlen = HANDSHAKE_NAME_LEN; size_t needlen = HANDSHAKE_NAME_LEN;
if (evbuffer_get_length(inbuf) < needlen) if (evbuffer_get_length(inbuf) < needlen)
@ -378,7 +375,7 @@ static ReadState readYb(tr_handshake* handshake, struct evbuffer* inbuf)
return READ_LATER; 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) 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), /* now send these: HASH('req1', S), HASH('req2', SKEY) xor HASH('req3', S),
* ENCRYPT(VC, crypto_provide, len(PadC), PadC, len(IA)), ENCRYPT(IA) */ * ENCRYPT(VC, crypto_provide, len(PadC), PadC, len(IA)), ENCRYPT(IA) */
outbuf = evbuffer_new(); evbuffer* const outbuf = evbuffer_new();
/* HASH('req1', S) */ /* HASH('req1', S) */
{ {
@ -718,7 +715,6 @@ static ReadState readYa(tr_handshake* handshake, struct evbuffer* inbuf)
uint8_t ya[KEY_LEN]; uint8_t ya[KEY_LEN];
uint8_t* walk; uint8_t* walk;
uint8_t outbuf[KEY_LEN + PadB_MAXLEN]; uint8_t outbuf[KEY_LEN + PadB_MAXLEN];
uint8_t const* myKey;
int len; int len;
dbgmsg(handshake, "in readYa... need %d, have %zu", KEY_LEN, evbuffer_get_length(inbuf)); 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 */ /* send our public key to the peer */
dbgmsg(handshake, "sending B->A: Diffie Hellman Yb, PadB"); dbgmsg(handshake, "sending B->A: Diffie Hellman Yb, PadB");
walk = outbuf; walk = outbuf;
myKey = tr_cryptoGetMyPublicKey(handshake->crypto, &len); uint8_t const* const myKey = tr_cryptoGetMyPublicKey(handshake->crypto, &len);
memcpy(walk, myKey, len); memcpy(walk, myKey, len);
walk += len; walk += len;
len = tr_rand_int(PadB_MAXLEN); 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]; obfuscatedTorrentHash[i] = req2[i] ^ req3[i];
} }
tr_torrent const* tor; tr_torrent const* const tor = tr_torrentFindFromObfuscatedHash(handshake->session, obfuscatedTorrentHash);
if ((tor = tr_torrentFindFromObfuscatedHash(handshake->session, obfuscatedTorrentHash)) != nullptr) if (tor != nullptr)
{ {
bool const clientIsSeed = tr_torrentIsSeed(tor); bool const clientIsSeed = tr_torrentIsSeed(tor);
bool const peerIsSeed = tr_peerMgrPeerIsSeed(tor, tr_peerIoGetAddress(handshake->io, nullptr)); 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) static ReadState readPadC(tr_handshake* handshake, struct evbuffer* inbuf)
{ {
char* padc;
uint16_t ia_len; uint16_t ia_len;
size_t const needlen = handshake->pad_c_len + sizeof(uint16_t); 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 */ /* 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_peerIoReadBytes(handshake->io, inbuf, padc, handshake->pad_c_len);
tr_free(padc); 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) static ReadState readIA(tr_handshake* handshake, struct evbuffer const* inbuf)
{ {
size_t const needlen = handshake->ia_len; 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); 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); tr_cryptoEncryptInit(handshake->crypto);
outbuf = evbuffer_new(); evbuffer* const outbuf = evbuffer_new();
{ {
/* send VC */ /* send VC */
@ -899,7 +892,7 @@ static ReadState readIA(tr_handshake* handshake, struct evbuffer const* inbuf)
} }
/* send crypto_select */ /* send crypto_select */
crypto_select = getCryptoSelect(handshake, handshake->crypto_provide); uint32_t const crypto_select = getCryptoSelect(handshake, handshake->crypto_provide);
if (crypto_select != 0) 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) static ReadState readPayloadStream(tr_handshake* handshake, struct evbuffer* inbuf)
{ {
handshake_parse_err_t i;
size_t const needlen = HANDSHAKE_SIZE; size_t const needlen = HANDSHAKE_SIZE;
dbgmsg(handshake, "reading payload stream... have %zu, need %zu", evbuffer_get_length(inbuf), needlen); 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 ... */ /* parse the handshake ... */
i = parseHandshake(handshake, inbuf); handshake_parse_err_t const i = parseHandshake(handshake, inbuf);
dbgmsg(handshake, "parseHandshake returned %d", i); dbgmsg(handshake, "parseHandshake returned %d", i);
if (i != HANDSHAKE_OK) 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. */ /* This peer probably doesn't speak uTP. */
tr_torrent* tor; tr_torrent* const tor = tr_peerIoHasTorrentHash(io) ?
tr_torrentFindFromHash(handshake->session, tr_peerIoGetTorrentHash(io)) :
if (tr_peerIoHasTorrentHash(io)) nullptr;
{
tor = tr_torrentFindFromHash(handshake->session, tr_peerIoGetTorrentHash(io));
}
else
{
tor = nullptr;
}
/* Don't mark a peer as non-uTP unless it's really a connect failure. */ /* Don't mark a peer as non-uTP unless it's really a connect failure. */
if ((errcode == ETIMEDOUT || errcode == ECONNREFUSED) && tr_isTorrent(tor)) if ((errcode == ETIMEDOUT || errcode == ECONNREFUSED) && tr_isTorrent(tor))
@ -1196,10 +1181,9 @@ tr_handshake* tr_handshakeNew(
tr_handshake_done_func done_func, tr_handshake_done_func done_func,
void* done_func_user_data) void* done_func_user_data)
{ {
tr_handshake* handshake;
tr_session* session = tr_peerIoGetSession(io); tr_session* session = tr_peerIoGetSession(io);
handshake = tr_new0(tr_handshake, 1); auto* const handshake = tr_new0(tr_handshake, 1);
handshake->io = io; handshake->io = io;
handshake->crypto = tr_peerIoGetCrypto(io); handshake->crypto = tr_peerIoGetCrypto(io);
handshake->encryptionMode = encryptionMode; handshake->encryptionMode = encryptionMode;

View file

@ -86,9 +86,7 @@ static void logVal(char const* func, int ret)
struct tr_natpmp* tr_natpmpInit(void) struct tr_natpmp* tr_natpmpInit(void)
{ {
struct tr_natpmp* nat; auto* const nat = tr_new0(struct tr_natpmp, 1);
nat = tr_new0(struct tr_natpmp, 1);
nat->state = TR_NATPMP_DISCOVER; nat->state = TR_NATPMP_DISCOVER;
nat->public_port = 0; nat->public_port = 0;
nat->private_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 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) if (is_enabled && nat->state == TR_NATPMP_DISCOVER)
{ {
int val = initnatpmp(&nat->natpmp, 0, 0); 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; return nat->is_mapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED;
case TR_NATPMP_DISCOVER: case TR_NATPMP_DISCOVER:
ret = TR_PORT_UNMAPPED; return TR_PORT_UNMAPPED;
break;
case TR_NATPMP_RECV_PUB: case TR_NATPMP_RECV_PUB:
case TR_NATPMP_SEND_MAP: case TR_NATPMP_SEND_MAP:
case TR_NATPMP_RECV_MAP: case TR_NATPMP_RECV_MAP:
ret = TR_PORT_MAPPING; return TR_PORT_MAPPING;
break;
case TR_NATPMP_SEND_UNMAP: case TR_NATPMP_SEND_UNMAP:
case TR_NATPMP_RECV_UNMAP: case TR_NATPMP_RECV_UNMAP:
ret = TR_PORT_UNMAPPING; return TR_PORT_UNMAPPING;
break;
default: default:
ret = TR_PORT_ERROR; return TR_PORT_ERROR;
break;
} }
return ret;
} }

View file

@ -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 tr_ptrArrayInsertSorted(tr_ptrArray* t, void* ptr, tr_voidptr_compare_func compare)
{ {
int pos;
int ret;
assertArrayIsSortedAndUnique(t, compare); assertArrayIsSortedAndUnique(t, compare);
pos = tr_ptrArrayLowerBound(t, ptr, compare, nullptr); int const pos = tr_ptrArrayLowerBound(t, ptr, compare, nullptr);
ret = tr_ptrArrayInsert(t, ptr, pos); int const ret = tr_ptrArrayInsert(t, ptr, pos);
assertIndexIsSortedAndUnique(t, ret, compare); assertIndexIsSortedAndUnique(t, ret, compare);
return ret; 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) static void* tr_ptrArrayRemoveSortedValue(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare)
{ {
int pos;
bool match;
void* ret = nullptr; void* ret = nullptr;
assertArrayIsSortedAndUnique(t, compare); assertArrayIsSortedAndUnique(t, compare);
pos = tr_ptrArrayLowerBound(t, ptr, compare, &match); bool match = false;
int const pos = tr_ptrArrayLowerBound(t, ptr, compare, &match);
if (match) if (match)
{ {

View file

@ -79,14 +79,12 @@ struct bootstrap_closure
static bool bootstrap_done(tr_session* session, int af) static bool bootstrap_done(tr_session* session, int af)
{ {
int status;
if (af == 0) if (af == 0)
{ {
return bootstrap_done(session, AF_INET) && bootstrap_done(session, AF_INET6); 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; 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 hints;
struct addrinfo* info; struct addrinfo* info;
struct addrinfo* infop;
char pp[10]; char pp[10];
int rc;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM; 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. */ /* No, just passing p + 1 to gai won't work. */
tr_snprintf(pp, sizeof(pp), "%d", (int)port); 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) if (rc != 0)
{ {
@ -134,8 +130,7 @@ static void bootstrap_from_name(char const* name, tr_port port, int af)
return; return;
} }
infop = info; addrinfo* infop = info;
while (infop != nullptr) while (infop != nullptr)
{ {
dht_ping_node(infop->ai_addr, infop->ai_addrlen); 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)) if (!bootstrap_done(cl->session, 0))
{ {
char* bootstrap_file;
tr_sys_file_t f = TR_BAD_SYS_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) if (bootstrap_file != nullptr)
{ {
@ -315,9 +309,7 @@ static void dht_bootstrap(void* closure)
int tr_dhtInit(tr_session* ss) int tr_dhtInit(tr_session* ss)
{ {
tr_variant benc; tr_variant benc;
int rc;
bool have_id = false; bool have_id = false;
char* dat_file;
uint8_t* nodes = nullptr; uint8_t* nodes = nullptr;
uint8_t* nodes6 = nullptr; uint8_t* nodes6 = nullptr;
uint8_t const* raw; uint8_t const* raw;
@ -337,8 +329,8 @@ int tr_dhtInit(tr_session* ss)
dht_debug = stderr; dht_debug = stderr;
} }
dat_file = tr_buildPath(ss->configDir, "dht.dat", nullptr); char* const dat_file = tr_buildPath(ss->configDir, "dht.dat", nullptr);
rc = tr_variantFromFile(&benc, TR_VARIANT_FMT_BENC, dat_file, nullptr) ? 0 : -1; int rc = tr_variantFromFile(&benc, TR_VARIANT_FMT_BENC, dat_file, nullptr) ? 0 : -1;
tr_free(dat_file); tr_free(dat_file);
if (rc == 0) 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) if (event == DHT_EVENT_VALUES || event == DHT_EVENT_VALUES6)
{ {
tr_torrent* tor;
tr_sessionLock(session_); tr_sessionLock(session_);
tor = tr_torrentFindFromHash(session_, info_hash);
tr_torrent* const tor = tr_torrentFindFromHash(session_, info_hash);
if (tor != nullptr && tr_torrentAllowsDHT(tor)) if (tor != nullptr && tr_torrentAllowsDHT(tor))
{ {
size_t n; size_t n = 0;
tr_pex* pex; tr_pex* const pex = event == DHT_EVENT_VALUES ? tr_peerMgrCompactToPex(data, data_len, nullptr, 0, &n) :
tr_peerMgrCompact6ToPex(data, data_len, nullptr, 0, &n);
if (event == DHT_EVENT_VALUES)
{
pex = tr_peerMgrCompactToPex(data, data_len, nullptr, 0, &n);
}
else
{
pex = tr_peerMgrCompact6ToPex(data, data_len, nullptr, 0, &n);
}
tr_peerMgrAddPex(tor, TR_PEER_FROM_DHT, pex, 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) static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce)
{ {
int rc;
int status;
int numnodes; int numnodes;
int ret = 0; int ret = 0;
@ -705,7 +686,7 @@ static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce)
return -1; return -1;
} }
status = tr_dhtStatus(tor->session, af, &numnodes); int const status = tr_dhtStatus(tor->session, af, &numnodes);
if (status == TR_DHT_STOPPED) 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) 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) if (rc >= 0)
{ {
tr_logAddTorInfo( tr_logAddTorInfo(