1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-21 21:57:01 +00:00

refactor: cppcoreguidelines-init-variables pt. 8 (#1993)

* refactor: fix uninit var warnings in rpcimpl
This commit is contained in:
Charles Kerr 2021-10-20 11:31:20 -05:00 committed by GitHub
parent 236a0965fa
commit 32395056e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -318,14 +318,10 @@ static char const* torrentRemove(
[[maybe_unused]] tr_variant* args_out,
[[maybe_unused]] struct tr_rpc_idle_data* idle_data)
{
bool deleteFlag;
auto delete_flag = bool{ false };
tr_variantDictFindBool(args_in, TR_KEY_delete_local_data, &delete_flag);
if (!tr_variantDictFindBool(args_in, TR_KEY_delete_local_data, &deleteFlag))
{
deleteFlag = false;
}
tr_rpc_callback_type type = deleteFlag ? TR_RPC_TORRENT_TRASHING : TR_RPC_TORRENT_REMOVING;
tr_rpc_callback_type type = delete_flag ? TR_RPC_TORRENT_TRASHING : TR_RPC_TORRENT_REMOVING;
for (auto* tor : getTorrents(session, args_in))
{
@ -333,7 +329,7 @@ static char const* torrentRemove(
if ((status & TR_RPC_NOREMOVE) == 0)
{
tr_torrentRemove(tor, deleteFlag, nullptr);
tr_torrentRemove(tor, delete_flag, nullptr);
}
}
@ -388,10 +384,10 @@ static void addLabels(tr_torrent const* tor, tr_variant* list)
static void addFileStats(tr_torrent const* tor, tr_variant* list)
{
tr_file_index_t n;
tr_info const* info = tr_torrentInfo(tor);
tr_file_stat* files = tr_torrentFiles(tor, &n);
auto n = tr_file_index_t{};
auto* files = tr_torrentFiles(tor, &n);
auto const* const info = tr_torrentInfo(tor);
for (tr_file_index_t i = 0; i < info->fileCount; ++i)
{
tr_file const* file = &info->files[i];
@ -406,10 +402,10 @@ static void addFileStats(tr_torrent const* tor, tr_variant* list)
static void addFiles(tr_torrent const* tor, tr_variant* list)
{
tr_file_index_t n;
tr_info const* info = tr_torrentInfo(tor);
tr_file_stat* files = tr_torrentFiles(tor, &n);
auto n = tr_file_index_t{};
auto* const files = tr_torrentFiles(tor, &n);
auto const* const info = tr_torrentInfo(tor);
for (tr_file_index_t i = 0; i < info->fileCount; ++i)
{
tr_file const* file = &info->files[i];
@ -480,7 +476,7 @@ static void addTrackerStats(tr_tracker_stat const* st, int n, tr_variant* list)
static void addPeers(tr_torrent* tor, tr_variant* list)
{
int peerCount;
auto peerCount = int{};
tr_peer_stat* peers = tr_torrentPeers(tor, &peerCount);
tr_variantInitList(list, peerCount);
@ -517,7 +513,7 @@ static void initField(
tr_variant* const initme,
tr_quark key)
{
char* str;
char* str = nullptr;
switch (key)
{
@ -806,7 +802,7 @@ static void initField(
case TR_KEY_trackerStats:
{
int n;
auto n = int{};
tr_tracker_stat* s = tr_torrentTrackers(tor, &n);
tr_variantInitList(initme, n);
addTrackerStats(s, n, initme);
@ -904,15 +900,15 @@ static char const* torrentGet(
if (tr_variantDictFindStr(args_in, TR_KEY_ids, &strVal, nullptr) && strcmp(strVal, "recently-active") == 0)
{
int n = 0;
tr_variant* d;
time_t const now = tr_time();
int const interval = RECENTLY_ACTIVE_SECONDS;
tr_variant* removed_out = tr_variantDictAddList(args_out, TR_KEY_removed, 0);
tr_variant* d = nullptr;
while ((d = tr_variantListChild(&session->removedTorrents, n)) != nullptr)
{
int64_t date;
int64_t id;
auto date = int64_t{};
auto id = int64_t{};
if (tr_variantDictFindInt(d, TR_KEY_date, &date) && date >= now - interval &&
tr_variantDictFindInt(d, TR_KEY_id, &id))
@ -938,7 +934,7 @@ static char const* torrentGet(
tr_quark* keys = tr_new(tr_quark, n);
for (size_t i = 0; i < n; ++i)
{
size_t len;
auto len = size_t{};
if (tr_variantGetStr(tr_variantListChild(fields, i), &strVal, &len))
{
keys[keyCount++] = tr_quark_new(std::string_view{ strVal, len });
@ -972,13 +968,14 @@ static char const* torrentGet(
static char const* setLabels(tr_torrent* tor, tr_variant* list)
{
size_t const n = tr_variantListSize(list);
char const* errmsg = nullptr;
auto labels = tr_labels_t{};
size_t const n = tr_variantListSize(list);
for (size_t i = 0; i < n; ++i)
{
char const* str;
size_t str_len;
char const* str = nullptr;
auto str_len = size_t{};
if (tr_variantGetStr(tr_variantListChild(list, i), &str, &str_len) && str != nullptr)
{
char* label = tr_strndup(str, str_len);
@ -1018,7 +1015,6 @@ static char const* setLabels(tr_torrent* tor, tr_variant* list)
static char const* setFilePriorities(tr_torrent* tor, int priority, tr_variant* list)
{
int64_t tmp;
int fileCount = 0;
size_t const n = tr_variantListSize(list);
char const* errmsg = nullptr;
@ -1028,6 +1024,7 @@ static char const* setFilePriorities(tr_torrent* tor, int priority, tr_variant*
{
for (size_t i = 0; i < n; ++i)
{
auto tmp = int64_t{};
if (tr_variantGetInt(tr_variantListChild(list, i), &tmp))
{
if (0 <= tmp && tmp < tor->info.fileCount)
@ -1060,16 +1057,17 @@ static char const* setFilePriorities(tr_torrent* tor, int priority, tr_variant*
static char const* setFileDLs(tr_torrent* tor, bool do_download, tr_variant* list)
{
int64_t tmp;
char const* errmsg = nullptr;
int fileCount = 0;
size_t const n = tr_variantListSize(list);
char const* errmsg = nullptr;
tr_file_index_t* files = tr_new0(tr_file_index_t, tor->info.fileCount);
if (n != 0) /* if argument list, process them */
{
for (size_t i = 0; i < n; ++i)
{
auto tmp = int64_t{};
if (tr_variantGetInt(tr_variantListChild(list, i), &tmp))
{
if (0 <= tmp && tmp < tor->info.fileCount)
@ -1148,23 +1146,18 @@ static void freeTrackers(tr_tracker_info* trackers, int n)
static char const* addTrackerUrls(tr_torrent* tor, tr_variant* urls)
{
int i;
int n;
int tier;
tr_tracker_info* trackers;
bool changed = false;
tr_info const* inf = tr_torrentInfo(tor);
char const* errmsg = nullptr;
/* make a working copy of the existing announce list */
n = inf->trackerCount;
trackers = tr_new0(tr_tracker_info, n + tr_variantListSize(urls));
tier = copyTrackers(trackers, inf->trackers, n);
auto const* const inf = tr_torrentInfo(tor);
int n = inf->trackerCount;
auto* const trackers = tr_new0(tr_tracker_info, n + tr_variantListSize(urls));
int tier = copyTrackers(trackers, inf->trackers, n);
/* and add the new ones */
i = 0;
tr_variant const* val;
auto i = int{ 0 };
auto changed = bool{ false };
tr_variant const* val = nullptr;
while ((val = tr_variantListChild(urls, i)) != nullptr)
{
char const* announce = nullptr;
@ -1196,22 +1189,21 @@ static char const* addTrackerUrls(tr_torrent* tor, tr_variant* urls)
static char const* replaceTrackers(tr_torrent* tor, tr_variant* urls)
{
tr_tracker_info* trackers;
bool changed = false;
tr_info const* inf = tr_torrentInfo(tor);
int const n = inf->trackerCount;
char const* errmsg = nullptr;
/* make a working copy of the existing announce list */
trackers = tr_new0(tr_tracker_info, n);
auto const* const inf = tr_torrentInfo(tor);
int const n = inf->trackerCount;
auto* const trackers = tr_new0(tr_tracker_info, n);
copyTrackers(trackers, inf->trackers, n);
/* make the substitutions... */
bool changed = false;
for (size_t i = 0, url_count = tr_variantListSize(urls); i + 1 < url_count; i += 2)
{
size_t len;
int64_t pos;
char const* newval;
auto len = size_t{};
auto pos = int64_t{};
char const* newval = nullptr;
if (tr_variantGetInt(tr_variantListChild(urls, i), &pos) &&
tr_variantGetStr(tr_variantListChild(urls, i + 1), &newval, &len) && tr_urlIsValidTracker(newval) && pos < n &&
@ -1248,10 +1240,10 @@ static char const* removeTrackers(tr_torrent* tor, tr_variant* ids)
/* remove the ones specified in the urls list */
int i = 0;
int t = 0;
tr_variant const* val;
tr_variant const* val = nullptr;
while ((val = tr_variantListChild(ids, i)) != nullptr)
{
int64_t pos;
auto pos = int64_t{};
if (tr_variantGetInt(val, &pos) && 0 <= pos && pos < n)
{
@ -1577,7 +1569,6 @@ static void gotNewBlocklist(
}
else /* successfully fetched the blocklist... */
{
int err;
z_stream stream;
char const* configDir = tr_sessionGetConfigDir(session);
size_t const buflen = 1024 * 128; /* 128 KiB buffer */
@ -1604,6 +1595,7 @@ static void gotNewBlocklist(
tr_error_clear(&error);
}
auto err = int{};
for (;;)
{
stream.next_out = static_cast<Bytef*>(buf);
@ -1675,17 +1667,13 @@ static char const* blocklistUpdate(
static void addTorrentImpl(struct tr_rpc_idle_data* data, tr_ctor* ctor)
{
int err;
int duplicate_id;
char const* result;
tr_torrent* tor;
tr_quark key;
err = 0;
duplicate_id = 0;
tor = tr_torrentNew(ctor, &err, &duplicate_id);
auto err = int{};
auto duplicate_id = int{};
tr_torrent* tor = tr_torrentNew(ctor, &err, &duplicate_id);
tr_ctorFree(ctor);
auto key = tr_quark{};
char const* result = "invalid or corrupt torrent file";
if (err == 0)
{
key = TR_KEY_torrent_added;
@ -1697,11 +1685,6 @@ static void addTorrentImpl(struct tr_rpc_idle_data* data, tr_ctor* ctor)
key = TR_KEY_torrent_duplicate;
result = "duplicate torrent";
}
else /* err == TR_PARSE_ERR */
{
key = 0;
result = "invalid or corrupt torrent file";
}
if (tor != nullptr && key != 0)
{
@ -1777,23 +1760,22 @@ static bool isCurlURL(char const* filename)
return strncmp(filename, "ftp://", 6) == 0 || strncmp(filename, "http://", 7) == 0 || strncmp(filename, "https://", 8) == 0;
}
static tr_file_index_t* fileListFromList(tr_variant* list, tr_file_index_t* setmeCount)
static auto fileListFromList(tr_variant* list)
{
size_t const childCount = tr_variantListSize(list);
tr_file_index_t n = 0;
tr_file_index_t* files = tr_new0(tr_file_index_t, childCount);
size_t const n = tr_variantListSize(list);
for (size_t i = 0; i < childCount; ++i)
auto files = std::vector<tr_file_index_t>{};
files.reserve(n);
auto file_index = int64_t{};
for (size_t i = 0; i < n; ++i)
{
int64_t intVal;
if (tr_variantGetInt(tr_variantListChild(list, i), &intVal))
if (tr_variantGetInt(tr_variantListChild(list, i), &file_index))
{
files[n++] = (tr_file_index_t)intVal;
files.push_back(file_index);
}
}
*setmeCount = n;
return files;
}
@ -1823,9 +1805,9 @@ static char const* torrentAdd(
return "download directory path is not absolute";
}
int64_t i;
bool boolVal;
tr_variant* l;
auto i = int64_t{};
auto boolVal = bool{};
tr_variant* l = nullptr;
tr_ctor* ctor = tr_ctorNew(session);
/* set the optional arguments */
@ -1855,42 +1837,32 @@ static char const* torrentAdd(
if (tr_variantDictFindList(args_in, TR_KEY_files_unwanted, &l))
{
tr_file_index_t fileCount;
tr_file_index_t* files = fileListFromList(l, &fileCount);
tr_ctorSetFilesWanted(ctor, files, fileCount, false);
tr_free(files);
auto const files = fileListFromList(l);
tr_ctorSetFilesWanted(ctor, std::data(files), std::size(files), false);
}
if (tr_variantDictFindList(args_in, TR_KEY_files_wanted, &l))
{
tr_file_index_t fileCount;
tr_file_index_t* files = fileListFromList(l, &fileCount);
tr_ctorSetFilesWanted(ctor, files, fileCount, true);
tr_free(files);
auto const files = fileListFromList(l);
tr_ctorSetFilesWanted(ctor, std::data(files), std::size(files), true);
}
if (tr_variantDictFindList(args_in, TR_KEY_priority_low, &l))
{
tr_file_index_t fileCount;
tr_file_index_t* files = fileListFromList(l, &fileCount);
tr_ctorSetFilePriorities(ctor, files, fileCount, TR_PRI_LOW);
tr_free(files);
auto const files = fileListFromList(l);
tr_ctorSetFilePriorities(ctor, std::data(files), std::size(files), TR_PRI_LOW);
}
if (tr_variantDictFindList(args_in, TR_KEY_priority_normal, &l))
{
tr_file_index_t fileCount;
tr_file_index_t* files = fileListFromList(l, &fileCount);
tr_ctorSetFilePriorities(ctor, files, fileCount, TR_PRI_NORMAL);
tr_free(files);
auto const files = fileListFromList(l);
tr_ctorSetFilePriorities(ctor, std::data(files), std::size(files), TR_PRI_NORMAL);
}
if (tr_variantDictFindList(args_in, TR_KEY_priority_high, &l))
{
tr_file_index_t fileCount;
tr_file_index_t* files = fileListFromList(l, &fileCount);
tr_ctorSetFilePriorities(ctor, files, fileCount, TR_PRI_HIGH);
tr_free(files);
auto const files = fileListFromList(l);
tr_ctorSetFilePriorities(ctor, std::data(files), std::size(files), TR_PRI_HIGH);
}
dbgmsg("torrentAdd: filename is \"%s\"", filename ? filename : " (null)");
@ -1908,8 +1880,8 @@ static char const* torrentAdd(
if (fname == nullptr)
{
size_t len;
auto* metainfo = static_cast<char*>(tr_base64_decode_str(metainfo_base64, &len));
auto len = size_t{};
auto* const metainfo = static_cast<char*>(tr_base64_decode_str(metainfo_base64, &len));
tr_ctorSetMetainfo(ctor, (uint8_t*)metainfo, len);
tr_free(metainfo);
}
@ -1954,10 +1926,10 @@ static char const* sessionSet(
return "incomplete torrents directory path is not absolute";
}
int64_t i;
double d;
bool boolVal;
char const* str;
auto boolVal = bool{};
auto d = double{};
auto i = int64_t{};
char const* str = nullptr;
if (tr_variantDictFindInt(args_in, TR_KEY_cache_size_mb, &i))
{
@ -2246,6 +2218,21 @@ static char const* sessionStats(
return nullptr;
}
static constexpr std::string_view getEncryptionModeString(tr_encryption_mode mode)
{
switch (mode)
{
case TR_CLEAR_PREFERRED:
return "tolerated"sv;
case TR_ENCRYPTION_REQUIRED:
return "required"sv;
default:
return "preferred"sv;
}
}
static void addSessionField(tr_session* s, tr_variant* d, tr_quark key)
{
switch (key)
@ -2463,27 +2450,8 @@ static void addSessionField(tr_session* s, tr_variant* d, tr_quark key)
break;
case TR_KEY_encryption:
{
auto str = std::string_view{};
switch (tr_sessionGetEncryption(s))
{
case TR_CLEAR_PREFERRED:
str = "tolerated"sv;
break;
case TR_ENCRYPTION_REQUIRED:
str = "required"sv;
break;
default:
str = "preferred"sv;
break;
}
tr_variantDictAddStr(d, key, str);
break;
}
tr_variantDictAddStr(d, key, getEncryptionModeString(tr_sessionGetEncryption(s)));
break;
case TR_KEY_session_id:
tr_variantDictAddStr(d, key, tr_session_id_get_current(s->session_id));
@ -2497,23 +2465,21 @@ static char const* sessionGet(
tr_variant* args_out,
[[maybe_unused]] struct tr_rpc_idle_data* idle_data)
{
tr_variant* fields;
tr_variant* fields = nullptr;
if (tr_variantDictFindList(args_in, TR_KEY_fields, &fields))
{
size_t const field_count = tr_variantListSize(fields);
for (size_t i = 0; i < field_count; ++i)
{
char const* field_name;
size_t field_name_len;
tr_quark field_id;
char const* field_name = nullptr;
auto field_name_len = size_t{};
if (!tr_variantGetStr(tr_variantListChild(fields, i), &field_name, &field_name_len))
{
continue;
}
auto field_id = tr_quark{};
if (!tr_quark_lookup(field_name, field_name_len, &field_id))
{
continue;
@ -2631,7 +2597,6 @@ void tr_rpc_request_exec_json(
tr_rpc_response_func callback,
void* callback_user_data)
{
char const* str;
tr_variant* const mutable_request = (tr_variant*)request;
tr_variant* args_in = tr_variantDictFind(mutable_request, TR_KEY_arguments);
char const* result = nullptr;
@ -2643,6 +2608,7 @@ void tr_rpc_request_exec_json(
}
/* parse the request */
char const* str = nullptr;
if (!tr_variantDictFindStr(mutable_request, TR_KEY_method, &str, nullptr))
{
result = "no method name";
@ -2666,13 +2632,12 @@ void tr_rpc_request_exec_json(
/* if we couldn't figure out which method to use, return an error */
if (result != nullptr)
{
int64_t tag;
tr_variant response;
auto response = tr_variant{};
tr_variantInitDict(&response, 3);
tr_variantDictAddDict(&response, TR_KEY_arguments, 0);
tr_variantDictAddStr(&response, TR_KEY_result, result);
auto tag = int64_t{};
if (tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
{
tr_variantDictAddInt(&response, TR_KEY_tag, tag);
@ -2684,12 +2649,9 @@ void tr_rpc_request_exec_json(
}
else if (method->immediate)
{
int64_t tag;
tr_variant response;
tr_variant* args_out;
auto response = tr_variant{};
tr_variantInitDict(&response, 3);
args_out = tr_variantDictAddDict(&response, TR_KEY_arguments, 0);
tr_variant* const args_out = tr_variantDictAddDict(&response, TR_KEY_arguments, 0);
result = (*method->func)(session, args_in, args_out, nullptr);
if (result == nullptr)
@ -2699,6 +2661,7 @@ void tr_rpc_request_exec_json(
tr_variantDictAddStr(&response, TR_KEY_result, result);
auto tag = int64_t{};
if (tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
{
tr_variantDictAddInt(&response, TR_KEY_tag, tag);
@ -2710,12 +2673,12 @@ void tr_rpc_request_exec_json(
}
else
{
int64_t tag;
struct tr_rpc_idle_data* data = tr_new0(struct tr_rpc_idle_data, 1);
data->session = session;
data->response = tr_new0(tr_variant, 1);
tr_variantInitDict(data->response, 3);
auto tag = int64_t{};
if (tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
{
tr_variantDictAddInt(data->response, TR_KEY_tag, tag);