From 0b1e12ac5be7a5b8e0c1f0353c56b03d9eb89ed8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 19 Oct 2021 11:09:38 -0500 Subject: [PATCH] refactor: fix uninit var warnings in upnp, utils, verify, watchdir (#1989) --- libtransmission/upnp.cc | 35 ++++------ libtransmission/utils.cc | 105 ++++++++++------------------ libtransmission/verify.cc | 18 ++--- libtransmission/watchdir-inotify.cc | 5 +- libtransmission/watchdir.cc | 14 ++-- 5 files changed, 63 insertions(+), 114 deletions(-) diff --git a/libtransmission/upnp.cc b/libtransmission/upnp.cc index 517128f46..1e3cf5209 100644 --- a/libtransmission/upnp.cc +++ b/libtransmission/upnp.cc @@ -81,8 +81,8 @@ void tr_upnpClose(tr_upnp* handle) static struct UPNPDev* tr_upnpDiscover(int msec) { - struct UPNPDev* ret; - bool have_err; + UPNPDev* ret = nullptr; + auto have_err = bool{}; #if (MINIUPNPC_API_VERSION >= 8) /* adds ipv6 and error args */ int err = UPNPDISCOVER_SUCCESS; @@ -109,7 +109,6 @@ static struct UPNPDev* tr_upnpDiscover(int msec) static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto) { - int err; char intClient[16]; char intPort[16]; char portStr[16]; @@ -120,7 +119,7 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto tr_snprintf(portStr, sizeof(portStr), "%d", handle->port); #if (MINIUPNPC_API_VERSION >= 10) /* adds remoteHost arg */ - err = UPNP_GetSpecificPortMappingEntry( + int const err = UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype, portStr, @@ -132,7 +131,7 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto nullptr /*enabled*/, nullptr /*duration*/); #elif (MINIUPNPC_API_VERSION >= 8) /* adds desc, enabled and leaseDuration args */ - err = UPNP_GetSpecificPortMappingEntry( + int const err = UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype, portStr, @@ -143,7 +142,7 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto nullptr /*enabled*/, nullptr /*duration*/); #else - err = UPNP_GetSpecificPortMappingEntry( + int const err = UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype, portStr, @@ -157,7 +156,6 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto static int tr_upnpAddPortMapping(tr_upnp const* handle, char const* proto, tr_port port, char const* desc) { - int err; int const old_errno = errno; char portStr[16]; errno = 0; @@ -165,7 +163,7 @@ static int tr_upnpAddPortMapping(tr_upnp const* handle, char const* proto, tr_po tr_snprintf(portStr, sizeof(portStr), "%d", (int)port); #if (MINIUPNPC_API_VERSION >= 8) - err = UPNP_AddPortMapping( + int err = UPNP_AddPortMapping( handle->urls.controlURL, handle->data.first.servicetype, portStr, @@ -176,7 +174,7 @@ static int tr_upnpAddPortMapping(tr_upnp const* handle, char const* proto, tr_po nullptr, nullptr); #else - err = UPNP_AddPortMapping( + int err = UPNP_AddPortMapping( handle->urls.controlURL, handle->data.first.servicetype, portStr, @@ -225,8 +223,6 @@ enum tr_port_forwarding tr_upnpPulse(tr_upnp* handle, tr_port port, bool isEnabled, bool doPortCheck) { - tr_port_forwarding ret; - if (isEnabled && handle->state == TR_UPNP_DISCOVER) { struct UPNPDev* devlist; @@ -331,25 +327,18 @@ tr_port_forwarding tr_upnpPulse(tr_upnp* handle, tr_port port, bool isEnabled, b switch (handle->state) { case TR_UPNP_DISCOVER: - ret = TR_PORT_UNMAPPED; - break; + return TR_PORT_UNMAPPED; case TR_UPNP_MAP: - ret = TR_PORT_MAPPING; - break; + return TR_PORT_MAPPING; case TR_UPNP_UNMAP: - ret = TR_PORT_UNMAPPING; - break; + return TR_PORT_UNMAPPING; case TR_UPNP_IDLE: - ret = handle->isMapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED; - break; + return handle->isMapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED; default: - ret = TR_PORT_ERROR; - break; + return TR_PORT_ERROR; } - - return ret; } diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index 12b5b8f95..fa8599436 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -281,14 +281,14 @@ void tr_timerAddMsec(struct event* timer, int msec) *** **/ +// TODO: return a std::vector<> uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error) { - tr_sys_path_info info; - tr_sys_file_t fd; - tr_error* my_error = nullptr; char const* const err_fmt = _("Couldn't read \"%1$s\": %2$s"); /* try to stat the file */ + auto info = tr_sys_path_info{}; + tr_error* my_error = nullptr; if (!tr_sys_path_get_info(path, 0, &info, &my_error)) { tr_logAddDebug(err_fmt, path, my_error->message); @@ -310,8 +310,7 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error) } /* Load the torrent file into our buffer */ - fd = tr_sys_file_open(path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error); - + tr_sys_file_t const fd = tr_sys_file_open(path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error); if (fd == TR_BAD_SYS_FILE) { tr_logAddError(err_fmt, path, my_error->message); @@ -320,7 +319,6 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error) } auto* buf = static_cast(tr_malloc(info.size + 1)); - if (!tr_sys_file_read(fd, buf, info.size, nullptr, &my_error)) { tr_logAddError(err_fmt, path, my_error->message); @@ -338,51 +336,42 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error) char* tr_buildPath(char const* first_element, ...) { - char const* element; - char* buf; - char* pch; - va_list vl; - size_t bufLen = 0; /* pass 1: allocate enough space for the string */ + va_list vl; va_start(vl, first_element); - element = first_element; - - while (element != nullptr) + auto bufLen = size_t{}; + for (char const* element = first_element; element != nullptr;) { bufLen += strlen(element) + 1; element = va_arg(vl, char const*); } - - pch = buf = tr_new(char, bufLen); va_end(vl); - + char* const buf = tr_new(char, bufLen); if (buf == nullptr) { return nullptr; } /* pass 2: build the string piece by piece */ + char* pch = buf; va_start(vl, first_element); - element = first_element; - - while (element != nullptr) + for (char const* element = first_element; element != nullptr;) { size_t const elementLen = strlen(element); - memcpy(pch, element, elementLen); - pch += elementLen; + pch = std::copy_n(element, elementLen, pch); *pch++ = TR_PATH_DELIMITER; element = va_arg(vl, char const*); } - va_end(vl); - /* terminate the string. if nonempty, eat the unwanted trailing slash */ + // if nonempty, eat the unwanted trailing slash if (pch != buf) { --pch; } + // zero-terminate the string *pch++ = '\0'; /* sanity checks & return */ @@ -390,19 +379,16 @@ char* tr_buildPath(char const* first_element, ...) return buf; } -struct tr_disk_space tr_getDirSpace(char const* dir) +tr_disk_space tr_getDirSpace(char const* dir) { - struct tr_disk_space disk_space = { -1, -1 }; - if (tr_str_is_empty(dir)) { errno = EINVAL; - return disk_space; + return { -1, -1 }; } - struct tr_device_info* info; - info = tr_device_info_create(dir); - disk_space = tr_device_info_get_disk_space(info); + auto* const info = tr_device_info_create(dir); + auto const disk_space = tr_device_info_get_disk_space(info); tr_device_info_free(info); return disk_space; } @@ -516,12 +502,9 @@ char const* tr_strcasestr(char const* haystack, char const* needle) char* tr_strdup_printf(char const* fmt, ...) { va_list ap; - char* ret; - va_start(ap, fmt); - ret = tr_strdup_vprintf(fmt, ap); + char* const ret = tr_strdup_vprintf(fmt, ap); va_end(ap); - return ret; } @@ -634,9 +617,6 @@ char* tr_strstrip(char* str) bool tr_str_has_suffix(char const* str, char const* suffix) { - size_t str_len; - size_t suffix_len; - if (str == nullptr) { return false; @@ -647,8 +627,8 @@ bool tr_str_has_suffix(char const* str, char const* suffix) return true; } - str_len = strlen(str); - suffix_len = strlen(suffix); + auto const str_len = strlen(str); + auto const suffix_len = strlen(suffix); if (str_len < suffix_len) { @@ -759,22 +739,17 @@ size_t tr_strlcpy(void* vdst, void const* vsrc, size_t siz) double tr_getRatio(uint64_t numerator, uint64_t denominator) { - double ratio; - if (denominator > 0) { - ratio = numerator / (double)denominator; - } - else if (numerator > 0) - { - ratio = TR_RATIO_INF; - } - else - { - ratio = TR_RATIO_NA; + return numerator / (double)denominator; } - return ratio; + if (numerator > 0) + { + return TR_RATIO_INF; + } + + return TR_RATIO_NA; } void tr_binary_to_hex(void const* vinput, void* voutput, size_t byte_length) @@ -882,9 +857,8 @@ bool tr_addressIsIP(char const* str) static int parse_port(char const* port, size_t port_len) { - char* tmp = tr_strndup(port, port_len); - char* end; - + char* const tmp = tr_strndup(port, port_len); + char* end = nullptr; long port_num = strtol(tmp, &end, 10); if (*end != '\0' || port_num <= 0 || port_num >= 65536) @@ -1067,9 +1041,9 @@ int tr_lowerBound( static char* strip_non_utf8(char const* in, size_t inlen) { - char const* end; - struct evbuffer* buf = evbuffer_new(); + evbuffer* const buf = evbuffer_new(); + char const* end = nullptr; while (!tr_utf8_validate(in, inlen, &end)) { int const good_len = end - in; @@ -1358,8 +1332,8 @@ struct number_range */ static bool parseNumberSection(std::string_view str, number_range& range) { - bool success; auto const error = errno; + auto success = bool{}; #if defined(HAVE_CHARCONV) // wants char*, so string_view::iterator don't work. make our own begin/end @@ -1438,11 +1412,11 @@ std::vector tr_parseNumberRange(std::string_view str) double tr_truncd(double x, int precision) { - char* pt; char buf[128]; tr_snprintf(buf, sizeof(buf), "%.*f", TR_ARG_TUPLE(DBL_DIG, x)); - if ((pt = strstr(buf, localeconv()->decimal_point)) != nullptr) + char* const pt = strstr(buf, localeconv()->decimal_point); + if (pt != nullptr) { pt[precision != 0 ? precision + 1 : 0] = '\0'; } @@ -1624,9 +1598,7 @@ enum static void formatter_init(formatter_units& units, size_t kilo, char const* kb, char const* mb, char const* gb, char const* tb) { - size_t value; - - value = kilo; + size_t value = kilo; units[TR_FMT_KB].name = tr_strdup(kb); units[TR_FMT_KB].value = value; @@ -1645,7 +1617,7 @@ static void formatter_init(formatter_units& units, size_t kilo, char const* kb, static char* formatter_get_size_str(formatter_units const& u, char* buf, size_t bytes, size_t buflen) { - formatter_unit const* unit; + formatter_unit const* unit = nullptr; if (bytes < u[1].value) { @@ -1667,7 +1639,7 @@ static char* formatter_get_size_str(formatter_units const& u, char* buf, size_t double value = (double)bytes / unit->value; char const* units = unit->name; - int precision = 0; + auto precision = int{}; if (unit->value == 1) { precision = 0; @@ -1754,13 +1726,12 @@ char* tr_formatter_mem_B(char* buf, size_t bytes_per_second, size_t buflen) void tr_formatter_get_units(void* vdict) { - tr_variant* l; auto* dict = static_cast(vdict); tr_variantDictReserve(dict, 6); tr_variantDictAddInt(dict, TR_KEY_memory_bytes, mem_units[TR_FMT_KB].value); - l = tr_variantDictAddList(dict, TR_KEY_memory_units, std::size(mem_units)); + tr_variant* l = tr_variantDictAddList(dict, TR_KEY_memory_units, std::size(mem_units)); for (auto const& unit : mem_units) { tr_variantListAddStr(l, unit.name); diff --git a/libtransmission/verify.cc b/libtransmission/verify.cc index 25c32401f..672f109b3 100644 --- a/libtransmission/verify.cc +++ b/libtransmission/verify.cc @@ -53,9 +53,6 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag) while (!*stopFlag && pieceIndex < tor->info.pieceCount) { - uint64_t leftInPiece; - uint64_t bytesThisPass; - uint64_t leftInFile; tr_file const* file = &tor->info.files[fileIndex]; /* if we're starting a new piece... */ @@ -75,16 +72,15 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag) } /* figure out how much we can read this pass */ - leftInPiece = tr_torPieceCountBytes(tor, pieceIndex) - piecePos; - leftInFile = file->length - filePos; - bytesThisPass = std::min(leftInFile, leftInPiece); + uint64_t leftInPiece = tr_torPieceCountBytes(tor, pieceIndex) - piecePos; + uint64_t leftInFile = file->length - filePos; + uint64_t bytesThisPass = std::min(leftInFile, leftInPiece); bytesThisPass = std::min(bytesThisPass, uint64_t{ buflen }); /* read a bit */ if (fd != TR_BAD_SYS_FILE) { - uint64_t numRead; - + auto numRead = uint64_t{}; if (tr_sys_file_read_at(fd, buffer, bytesThisPass, filePos, &numRead, nullptr) && numRead > 0) { bytesThisPass = numRead; @@ -102,12 +98,10 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag) /* if we're finishing a piece... */ if (leftInPiece == 0) { - time_t now; - bool hasPiece; uint8_t hash[SHA_DIGEST_LENGTH]; tr_sha1_final(sha, hash); - hasPiece = memcmp(hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH) == 0; + bool const hasPiece = memcmp(hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH) == 0; if (hasPiece || hadPiece) { @@ -116,7 +110,7 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag) } tr_torrentSetPieceChecked(tor, pieceIndex); - now = tr_time(); + time_t const now = tr_time(); tor->anyDate = now; /* sleeping even just a few msec per second goes a long diff --git a/libtransmission/watchdir-inotify.cc b/libtransmission/watchdir-inotify.cc index 69a7b553f..d29dae025 100644 --- a/libtransmission/watchdir-inotify.cc +++ b/libtransmission/watchdir-inotify.cc @@ -71,12 +71,12 @@ static void tr_watchdir_inotify_on_event(struct bufferevent* event, void* contex tr_watchdir_inotify const* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle)); #endif struct inotify_event ev; - size_t nread; size_t name_size = NAME_MAX + 1; char* name = tr_new(char, name_size); /* Read the size of the struct excluding name into buf. Guaranteed to have at least sizeof(ev) available */ + auto nread = size_t{}; while ((nread = bufferevent_read(event, &ev, sizeof(ev))) != 0) { if (nread == (size_t)-1) @@ -153,9 +153,8 @@ static void tr_watchdir_inotify_free(tr_watchdir_backend* backend_base) tr_watchdir_backend* tr_watchdir_inotify_new(tr_watchdir_t handle) { char const* const path = tr_watchdir_get_path(handle); - tr_watchdir_inotify* backend; - backend = tr_new0(tr_watchdir_inotify, 1); + auto* const backend = tr_new0(tr_watchdir_inotify, 1); backend->base.free_func = &tr_watchdir_inotify_free; backend->infd = -1; backend->inwd = -1; diff --git a/libtransmission/watchdir.cc b/libtransmission/watchdir.cc index 631b6c4aa..b80b65039 100644 --- a/libtransmission/watchdir.cc +++ b/libtransmission/watchdir.cc @@ -172,9 +172,7 @@ static void tr_watchdir_on_retry_timer([[maybe_unused]] evutil_socket_t fd, [[ma static tr_watchdir_retry* tr_watchdir_retry_new(tr_watchdir_t handle, char const* name) { - tr_watchdir_retry* retry; - - retry = tr_new0(tr_watchdir_retry, 1); + auto* const retry = tr_new0(tr_watchdir_retry, 1); retry->handle = handle; retry->name = tr_strdup(name); retry->timer = evtimer_new(handle->event_base, &tr_watchdir_on_retry_timer, retry); @@ -225,9 +223,7 @@ tr_watchdir_t tr_watchdir_new( struct event_base* event_base, bool force_generic) { - tr_watchdir_t handle; - - handle = tr_new0(struct tr_watchdir, 1); + auto* handle = tr_new0(struct tr_watchdir, 1); handle->path = tr_strdup(path); handle->callback = callback; handle->callback_user_data = callback_user_data; @@ -327,18 +323,18 @@ void tr_watchdir_process(tr_watchdir_t handle, char const* name) void tr_watchdir_scan(tr_watchdir_t handle, std::unordered_set* dir_entries) { - tr_sys_dir_t dir; - char const* name; auto new_dir_entries = std::unordered_set{}; tr_error* error = nullptr; - if ((dir = tr_sys_dir_open(handle->path, &error)) == TR_BAD_SYS_DIR) + tr_sys_dir_t const dir = tr_sys_dir_open(handle->path, &error); + if (dir == TR_BAD_SYS_DIR) { log_error("Failed to open directory \"%s\" (%d): %s", handle->path, error->code, error->message); tr_error_free(error); return; } + char const* name = nullptr; while ((name = tr_sys_dir_read_name(dir, &error)) != nullptr) { if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)