refactor: cppcoreguidelines-init-variables pt. 10 (#2006)

* refactor: uninit vars in peer-io.cc

* refactor: uninit vars in rpc-server.cc

* refactor: uninit vars in port-forwarding.cc

* refactor: uninit vars in torrent-ctor.cc
This commit is contained in:
Charles Kerr 2021-10-22 08:51:36 -05:00 committed by GitHub
parent af38bb1dc3
commit 9e716bb9dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 137 deletions

View File

@ -97,7 +97,7 @@ static struct tr_datatype* datatype_pool = nullptr;
static struct tr_datatype* datatype_new(void)
{
struct tr_datatype* ret;
tr_datatype* ret = nullptr;
if (datatype_pool == nullptr)
{
@ -121,9 +121,9 @@ static void datatype_free(struct tr_datatype* datatype)
static void peer_io_pull_datatype(tr_peerIo* io)
{
struct tr_datatype* tmp;
auto* const tmp = io->outbuf_datatypes;
if ((tmp = io->outbuf_datatypes) != nullptr)
if (tmp != nullptr)
{
io->outbuf_datatypes = tmp->next;
datatype_free(tmp);
@ -132,9 +132,9 @@ static void peer_io_pull_datatype(tr_peerIo* io)
static void peer_io_push_datatype(tr_peerIo* io, struct tr_datatype* datatype)
{
struct tr_datatype* tmp;
tr_datatype* tmp = io->outbuf_datatypes;
if ((tmp = io->outbuf_datatypes) != nullptr)
if (tmp != nullptr)
{
while (tmp->next != nullptr)
{
@ -191,23 +191,21 @@ static void didWriteWrapper(tr_peerIo* io, unsigned int bytes_transferred)
static void canReadWrapper(tr_peerIo* io)
{
bool err = false;
bool done = false;
tr_session* session;
dbgmsg(io, "canRead");
tr_peerIoRef(io);
session = io->session;
tr_session* const session = io->session;
/* try to consume the input buffer */
if (io->canRead != nullptr)
{
uint64_t const now = tr_time_msec();
tr_sessionLock(session);
auto const now = tr_time_msec();
auto done = bool{ false };
auto err = bool{ false };
while (!done && !err)
{
size_t piece = 0;
@ -270,19 +268,14 @@ static void event_read_cb(evutil_socket_t fd, [[maybe_unused]] short event, void
TR_ASSERT(tr_isPeerIo(io));
TR_ASSERT(io->socket.type == TR_PEER_SOCKET_TYPE_TCP);
int res;
int e;
/* Limit the input buffer to 256K, so it doesn't grow too large */
unsigned int howmuch;
unsigned int curlen;
tr_direction const dir = TR_DOWN;
unsigned int const max = 256 * 1024;
io->pendingEvents &= ~EV_READ;
curlen = evbuffer_get_length(io->inbuf);
howmuch = curlen >= max ? 0 : max - curlen;
unsigned int const curlen = evbuffer_get_length(io->inbuf);
unsigned int howmuch = curlen >= max ? 0 : max - curlen;
howmuch = io->bandwidth->clamp(TR_DOWN, howmuch);
dbgmsg(io, "libevent says this peer is ready to read");
@ -295,8 +288,8 @@ static void event_read_cb(evutil_socket_t fd, [[maybe_unused]] short event, void
}
EVUTIL_SET_SOCKET_ERROR(0);
res = evbuffer_read(io->inbuf, fd, (int)howmuch);
e = EVUTIL_SOCKET_ERROR();
auto const res = evbuffer_read(io->inbuf, fd, (int)howmuch);
int const e = EVUTIL_SOCKET_ERROR();
if (res > 0)
{
@ -307,7 +300,6 @@ static void event_read_cb(evutil_socket_t fd, [[maybe_unused]] short event, void
}
else
{
char errstr[512];
short what = BEV_EVENT_READING;
if (res == 0) /* EOF */
@ -325,6 +317,7 @@ static void event_read_cb(evutil_socket_t fd, [[maybe_unused]] short event, void
what |= BEV_EVENT_ERROR;
}
char errstr[512];
dbgmsg(
io,
"event_read_cb got an error. res is %d, what is %hd, errno is %d (%s)",
@ -342,13 +335,11 @@ static void event_read_cb(evutil_socket_t fd, [[maybe_unused]] short event, void
static int tr_evbuffer_write(tr_peerIo* io, int fd, size_t howmuch)
{
int e;
int n;
char errstr[256];
EVUTIL_SET_SOCKET_ERROR(0);
n = evbuffer_write_atmost(io->outbuf, fd, howmuch);
e = EVUTIL_SOCKET_ERROR();
int const n = evbuffer_write_atmost(io->outbuf, fd, howmuch);
int const e = EVUTIL_SOCKET_ERROR();
dbgmsg(io, "wrote %d to peer (%s)", n, (n == -1 ? tr_net_strerror(errstr, sizeof(errstr), e) : ""));
return n;
@ -361,12 +352,9 @@ static void event_write_cb(evutil_socket_t fd, [[maybe_unused]] short event, voi
TR_ASSERT(tr_isPeerIo(io));
TR_ASSERT(io->socket.type == TR_PEER_SOCKET_TYPE_TCP);
int res = 0;
int e;
short what = BEV_EVENT_WRITING;
size_t howmuch;
tr_direction const dir = TR_UP;
char errstr[1024];
auto const dir = TR_UP;
auto res = int{ 0 };
auto what = short{ BEV_EVENT_WRITING };
io->pendingEvents &= ~EV_WRITE;
@ -374,7 +362,7 @@ static void event_write_cb(evutil_socket_t fd, [[maybe_unused]] short event, voi
/* Write as much as possible, since the socket is non-blocking, write() will
* return if it can't write any more data without blocking */
howmuch = io->bandwidth->clamp(dir, evbuffer_get_length(io->outbuf));
size_t const howmuch = io->bandwidth->clamp(dir, evbuffer_get_length(io->outbuf));
/* if we don't have any bandwidth left, stop writing */
if (howmuch < 1)
@ -385,7 +373,7 @@ static void event_write_cb(evutil_socket_t fd, [[maybe_unused]] short event, voi
EVUTIL_SET_SOCKET_ERROR(0);
res = tr_evbuffer_write(io, fd, howmuch);
e = EVUTIL_SOCKET_ERROR();
int const e = EVUTIL_SOCKET_ERROR();
if (res == -1)
{
@ -425,6 +413,7 @@ RESCHEDULE:
return;
FAIL:
char errstr[1024];
tr_net_strerror(errstr, sizeof(errstr), e);
dbgmsg(io, "event_write_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr);
@ -502,11 +491,9 @@ static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch);
static void utp_on_writable(tr_peerIo* io)
{
int n;
dbgmsg(io, "libutp says this peer is ready to write");
n = tr_peerIoTryWrite(io, SIZE_MAX);
int const n = tr_peerIoTryWrite(io, SIZE_MAX);
tr_peerIoSetEnabled(io, TR_UP, n != 0 && evbuffer_get_length(io->outbuf) != 0);
}
@ -1114,8 +1101,7 @@ static inline void processBuffer(
static void addDatatype(tr_peerIo* io, size_t byteCount, bool isPieceData)
{
struct tr_datatype* d;
d = datatype_new();
auto* const d = datatype_new();
d->isPieceData = isPieceData;
d->length = byteCount;
peer_io_push_datatype(io, d);
@ -1236,14 +1222,14 @@ void tr_peerIoReadBytes(tr_peerIo* io, struct evbuffer* inbuf, void* bytes, size
void tr_peerIoReadUint16(tr_peerIo* io, struct evbuffer* inbuf, uint16_t* setme)
{
uint16_t tmp;
auto tmp = uint16_t{};
tr_peerIoReadBytes(io, inbuf, &tmp, sizeof(uint16_t));
*setme = ntohs(tmp);
}
void tr_peerIoReadUint32(tr_peerIo* io, struct evbuffer* inbuf, uint32_t* setme)
{
uint32_t tmp;
auto tmp = uint32_t{};
tr_peerIoReadBytes(io, inbuf, &tmp, sizeof(uint32_t));
*setme = ntohl(tmp);
}
@ -1267,7 +1253,7 @@ void tr_peerIoDrain(tr_peerIo* io, struct evbuffer* inbuf, size_t byteCount)
static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
{
int res = 0;
auto res = int{};
if ((howmuch = io->bandwidth->clamp(TR_DOWN, howmuch)) != 0)
{
@ -1286,12 +1272,11 @@ static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
case TR_PEER_SOCKET_TYPE_TCP:
{
int e;
char err_buf[512];
EVUTIL_SET_SOCKET_ERROR(0);
res = evbuffer_read(io->inbuf, io->socket.handle.tcp, (int)howmuch);
e = EVUTIL_SOCKET_ERROR();
int const e = EVUTIL_SOCKET_ERROR();
dbgmsg(io, "read %d from peer (%s)", res, res == -1 ? tr_net_strerror(err_buf, sizeof(err_buf), e) : "");
@ -1333,8 +1318,9 @@ static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch)
{
int n = 0;
size_t const old_len = evbuffer_get_length(io->outbuf);
auto const old_len = size_t{ evbuffer_get_length(io->outbuf) };
auto n = int{};
dbgmsg(io, "in tr_peerIoTryWrite %zu", howmuch);
if (howmuch > old_len)
@ -1353,11 +1339,9 @@ static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch)
case TR_PEER_SOCKET_TYPE_TCP:
{
int e;
EVUTIL_SET_SOCKET_ERROR(0);
n = tr_evbuffer_write(io, io->socket.handle.tcp, howmuch);
e = EVUTIL_SOCKET_ERROR();
int const e = EVUTIL_SOCKET_ERROR();
if (n > 0)
{

View File

@ -72,9 +72,6 @@ static char const* getNatStateStr(int state)
static void natPulse(tr_shared* s, bool do_check)
{
int oldStatus;
int newStatus;
tr_port public_peer_port;
tr_port const private_peer_port = s->session->private_peer_port;
bool const is_enabled = s->isEnabled && !s->isShuttingDown;
@ -88,8 +85,9 @@ static void natPulse(tr_shared* s, bool do_check)
s->upnp = tr_upnpInit();
}
oldStatus = tr_sharedTraversalStatus(s);
auto const old_status = tr_sharedTraversalStatus(s);
auto public_peer_port = tr_port{};
s->natpmpStatus = tr_natpmpPulse(s->natpmp, private_peer_port, is_enabled, &public_peer_port);
if (s->natpmpStatus == TR_PORT_MAPPED)
@ -99,15 +97,15 @@ static void natPulse(tr_shared* s, bool do_check)
s->upnpStatus = tr_upnpPulse(s->upnp, private_peer_port, is_enabled, do_check);
newStatus = tr_sharedTraversalStatus(s);
auto const new_status = tr_sharedTraversalStatus(s);
if (newStatus != oldStatus)
if (new_status != old_status)
{
tr_logAddNamedInfo(
getKey(),
_("State changed from \"%1$s\" to \"%2$s\""),
getNatStateStr(oldStatus),
getNatStateStr(newStatus));
getNatStateStr(old_status),
getNatStateStr(new_status));
}
}

View File

@ -129,14 +129,13 @@ static auto extract_parts_from_multipart(struct evkeyvalq const* headers, struct
while (delim != nullptr)
{
size_t part_len;
char const* part = delim + boundary_len;
inlen -= part - in;
in = part;
delim = tr_memmem(in, inlen, boundary, boundary_len);
part_len = delim != nullptr ? (size_t)(delim - part) : inlen;
size_t part_len = delim != nullptr ? (size_t)(delim - part) : inlen;
if (part_len != 0)
{
@ -198,22 +197,20 @@ static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* serv
for (auto const& p : parts)
{
auto const& body = p.body;
size_t body_len = std::size(body);
tr_variant top;
tr_variant* args;
tr_variant test;
bool have_source = false;
auto body_len = std::size(body);
if (body_len >= 2 && memcmp(&body[body_len - 2], "\r\n", 2) == 0)
{
body_len -= 2;
}
auto top = tr_variant{};
tr_variantInitDict(&top, 2);
tr_variantDictAddStr(&top, TR_KEY_method, "torrent-add");
args = tr_variantDictAddDict(&top, TR_KEY_arguments, 2);
auto* const args = tr_variantDictAddDict(&top, TR_KEY_arguments, 2);
tr_variantDictAddBool(args, TR_KEY_paused, paused);
auto test = tr_variant{};
auto have_source = bool{ false };
if (tr_urlIsValid(body.c_str(), body_len))
{
tr_variantDictAddRaw(args, TR_KEY_filename, body.c_str(), body_len);
@ -297,15 +294,12 @@ static void add_response(
}
else
{
int state;
struct evbuffer_iovec iovec[1];
void* content_ptr = evbuffer_pullup(content, -1);
size_t const content_len = evbuffer_get_length(content);
if (!server->isStreamInitialized)
{
int compressionLevel;
server->isStreamInitialized = true;
server->stream.zalloc = (alloc_func)Z_NULL;
server->stream.zfree = (free_func)Z_NULL;
@ -314,9 +308,9 @@ static void add_response(
/* zlib's manual says: "Add 16 to windowBits to write a simple gzip header
* and trailer around the compressed data instead of a zlib wrapper." */
#ifdef TR_LIGHTWEIGHT
compressionLevel = Z_DEFAULT_COMPRESSION;
int const compressionLevel = Z_DEFAULT_COMPRESSION;
#else
compressionLevel = Z_BEST_COMPRESSION;
int const compressionLevel = Z_BEST_COMPRESSION;
#endif
deflateInit2(&server->stream, compressionLevel, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
}
@ -330,7 +324,7 @@ static void add_response(
evbuffer_reserve_space(out, content_len, iovec, 1);
server->stream.next_out = static_cast<Bytef*>(iovec[0].iov_base);
server->stream.avail_out = iovec[0].iov_len;
state = deflate(&server->stream, Z_FINISH);
auto const state = deflate(&server->stream, Z_FINISH);
if (state == Z_STREAM_END)
{
@ -381,12 +375,9 @@ static void serve_file(struct evhttp_request* req, struct tr_rpc_server* server,
}
else
{
void* file;
size_t file_len;
auto file_len = size_t{};
tr_error* error = nullptr;
file_len = 0;
file = tr_loadFile(filename, &file_len, &error);
void* const file = tr_loadFile(filename, &file_len, &error);
if (file == nullptr)
{
@ -397,14 +388,12 @@ static void serve_file(struct evhttp_request* req, struct tr_rpc_server* server,
}
else
{
struct evbuffer* content;
struct evbuffer* out;
time_t const now = tr_time();
auto const now = tr_time();
content = evbuffer_new();
auto* const content = evbuffer_new();
evbuffer_add_reference(content, file, file_len, evbuffer_ref_cleanup_tr_free, file);
out = evbuffer_new();
auto* const out = evbuffer_new();
evhttp_add_header(req->output_headers, "Content-Type", mimetype_guess(filename));
add_time_header(req->output_headers, "Date", now);
add_time_header(req->output_headers, "Expires", now + (24 * 60 * 60));
@ -437,12 +426,10 @@ static void handle_web_client(struct evhttp_request* req, struct tr_rpc_server*
}
else
{
char* pch;
char* subpath;
subpath = tr_strdup(req->uri + strlen(server->url) + 4);
if ((pch = strchr(subpath, '?')) != nullptr)
// TODO: string_view
char* const subpath = tr_strdup(req->uri + strlen(server->url) + 4);
char* pch = strchr(subpath, '?');
if (pch != nullptr)
{
*pch = '\0';
}
@ -489,11 +476,10 @@ static void rpc_response_func([[maybe_unused]] tr_session* session, tr_variant*
static void handle_rpc_from_json(struct evhttp_request* req, struct tr_rpc_server* server, char const* json, size_t json_len)
{
tr_variant top;
bool have_content = tr_variantFromJson(&top, json, json_len) == 0;
struct rpc_response_data* data;
auto top = tr_variant{};
auto const have_content = tr_variantFromJson(&top, json, json_len) == 0;
data = tr_new0(struct rpc_response_data, 1);
auto* const data = tr_new0(struct rpc_response_data, 1);
data->req = req;
data->server = server;
@ -609,10 +595,6 @@ static void handle_request(struct evhttp_request* req, void* arg)
if (req != nullptr && req->evcon != nullptr)
{
char const* auth;
char* user = nullptr;
char* pass = nullptr;
evhttp_add_header(req->output_headers, "Server", MY_REALM);
if (server->isAntiBruteForceEnabled && server->loginattempts >= server->antiBruteForceThreshold)
@ -648,7 +630,9 @@ static void handle_request(struct evhttp_request* req, void* arg)
return;
}
auth = evhttp_find_header(req->input_headers, "Authorization");
char const* const auth = evhttp_find_header(req->input_headers, "Authorization");
char* user = nullptr;
char* pass = nullptr;
if (auth != nullptr && evutil_ascii_strncasecmp(auth, "basic ", 6) == 0)
{
@ -1119,12 +1103,12 @@ static void missing_settings_key(tr_quark const q)
tr_rpc_server* tr_rpcInit(tr_session* session, tr_variant* settings)
{
bool boolVal;
int64_t i;
char const* str;
tr_address address;
auto address = tr_address{};
auto boolVal = bool{};
auto i = int64_t{};
char const* str = nullptr;
tr_rpc_server* s = new tr_rpc_server{};
tr_rpc_server* const s = new tr_rpc_server{};
s->session = session;
tr_quark key = TR_KEY_rpc_enabled;
@ -1150,8 +1134,7 @@ tr_rpc_server* tr_rpcInit(tr_session* session, tr_variant* settings)
}
key = TR_KEY_rpc_url;
size_t url_len;
auto url_len = size_t{};
if (!tr_variantDictFindStr(settings, key, &str, &url_len))
{
missing_settings_key(key);

View File

@ -82,10 +82,8 @@ static void clearMetainfo(tr_ctor* ctor)
int tr_ctorSetMetainfo(tr_ctor* ctor, void const* metainfo, size_t len)
{
int err;
clearMetainfo(ctor);
err = tr_variantFromBenc(&ctor->metainfo, metainfo, len);
auto const err = tr_variantFromBenc(&ctor->metainfo, metainfo, len);
ctor->isSet_metainfo = err == 0;
return err;
}
@ -97,7 +95,8 @@ char const* tr_ctorGetSourceFile(tr_ctor const* ctor)
int tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, char const* magnet_link)
{
int err;
auto err = int{};
tr_magnet_info* magnet_info = tr_magnetParse(magnet_link);
if (magnet_info == nullptr)
@ -106,12 +105,10 @@ int tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, char const* magnet_link)
}
else
{
size_t len;
tr_variant tmp;
char* str;
auto tmp = tr_variant{};
auto len = size_t{};
tr_magnetCreateMetainfo(magnet_info, &tmp);
str = tr_variantToStr(&tmp, TR_VARIANT_FMT_BENC, &len);
char* const str = tr_variantToStr(&tmp, TR_VARIANT_FMT_BENC, &len);
err = tr_ctorSetMetainfo(ctor, (uint8_t const*)str, len);
tr_free(str);
@ -124,12 +121,10 @@ int tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, char const* magnet_link)
int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename)
{
uint8_t* metainfo;
size_t len;
int err;
metainfo = tr_loadFile(filename, &len, nullptr);
auto len = size_t{};
auto* const metainfo = tr_loadFile(filename, &len, nullptr);
auto err = int{};
if (metainfo != nullptr && len != 0)
{
err = tr_ctorSetMetainfo(ctor, metainfo, len);
@ -145,11 +140,11 @@ int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename)
/* if no `name' field was set, then set it from the filename */
if (ctor->isSet_metainfo)
{
tr_variant* info;
tr_variant* info = nullptr;
if (tr_variantDictFindDict(&ctor->metainfo, TR_KEY_info, &info))
{
char const* name;
char const* name = nullptr;
if (!tr_variantDictFindStr(info, TR_KEY_name_utf_8, &name, nullptr) &&
!tr_variantDictFindStr(info, TR_KEY_name, &name, nullptr))
@ -176,21 +171,8 @@ int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename)
int tr_ctorSetMetainfoFromHash(tr_ctor* ctor, char const* hashString)
{
int err;
char const* filename;
filename = tr_sessionFindTorrentFile(ctor->session, hashString);
if (filename == nullptr)
{
err = EINVAL;
}
else
{
err = tr_ctorSetMetainfoFromFile(ctor, filename);
}
return err;
char const* const filename = tr_sessionFindTorrentFile(ctor->session, hashString);
return filename == nullptr ? EINVAL : tr_ctorSetMetainfoFromFile(ctor, filename);
}
/***
@ -199,8 +181,8 @@ int tr_ctorSetMetainfoFromHash(tr_ctor* ctor, char const* hashString)
void tr_ctorSetFilePriorities(tr_ctor* ctor, tr_file_index_t const* files, tr_file_index_t fileCount, tr_priority_t priority)
{
tr_file_index_t** myfiles;
tr_file_index_t* mycount;
tr_file_index_t** myfiles = nullptr;
tr_file_index_t* mycount = nullptr;
switch (priority)
{