mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
refactor: modernize-avoid-c-arrays pt. 2 (#3713)
This commit is contained in:
@@ -26,7 +26,6 @@ Checks: >
|
||||
-misc-no-recursion,
|
||||
-misc-non-private-member-variables-in-classes,
|
||||
modernize-*,
|
||||
-modernize-avoid-c-arrays,
|
||||
-modernize-use-trailing-return-type,
|
||||
performance-*,
|
||||
portability-*,
|
||||
|
||||
@@ -239,8 +239,8 @@ void tr_tracker_udp_announce(
|
||||
|
||||
void tr_tracker_udp_start_shutdown(tr_session* session);
|
||||
|
||||
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, char const* log_name);
|
||||
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, std::string_view log_name);
|
||||
|
||||
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, char const* log_name);
|
||||
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, std::string_view log_name);
|
||||
|
||||
tr_interned_string tr_announcerGetKey(tr_url_parsed_t const& parsed);
|
||||
|
||||
@@ -158,7 +158,7 @@ static void verboseLog(std::string_view description, tr_direction direction, std
|
||||
|
||||
static auto constexpr MaxBencDepth = 8;
|
||||
|
||||
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, char const* log_name)
|
||||
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, std::string_view log_name)
|
||||
{
|
||||
verboseLog("Announce response:", TR_DOWN, benc);
|
||||
|
||||
@@ -308,7 +308,7 @@ struct announce_data
|
||||
uint8_t requests_sent_count;
|
||||
uint8_t requests_answered_count;
|
||||
|
||||
char log_name[128];
|
||||
std::string log_name;
|
||||
};
|
||||
|
||||
static bool handleAnnounceResponse(tr_web::FetchResponse const& web_response, tr_announce_response* const response)
|
||||
@@ -407,7 +407,7 @@ void tr_tracker_http_announce(
|
||||
d->response_func = response_func;
|
||||
d->response_func_user_data = response_func_user_data;
|
||||
d->info_hash = request->info_hash;
|
||||
tr_strlcpy(d->log_name, request->log_name, sizeof(d->log_name));
|
||||
d->log_name = request->log_name;
|
||||
|
||||
/* There are two alternative techniques for announcing both IPv4 and
|
||||
IPv6 addresses. Previous version of BEP-7 suggests adding "ipv4="
|
||||
@@ -502,7 +502,7 @@ void tr_tracker_http_announce(
|
||||
*****
|
||||
****/
|
||||
|
||||
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, char const* log_name)
|
||||
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, std::string_view log_name)
|
||||
{
|
||||
verboseLog("Scrape response:", TR_DOWN, benc);
|
||||
|
||||
@@ -611,7 +611,7 @@ struct scrape_data
|
||||
tr_scrape_response response;
|
||||
tr_scrape_response_func response_func;
|
||||
void* response_func_user_data;
|
||||
char log_name[128];
|
||||
std::string log_name;
|
||||
};
|
||||
|
||||
static void onScrapeDone(tr_web::FetchResponse const& web_response)
|
||||
@@ -677,7 +677,7 @@ void tr_tracker_http_scrape(
|
||||
d->response.rows[i].downloads = -1;
|
||||
}
|
||||
|
||||
tr_strlcpy(d->log_name, request->log_name, sizeof(d->log_name));
|
||||
d->log_name = request->log_name;
|
||||
|
||||
auto scrape_url = tr_pathbuf{};
|
||||
scrape_url_new(scrape_url, request);
|
||||
|
||||
@@ -130,6 +130,7 @@ std::vector<uint8_t> tr_completion::createPieceBitfield() const
|
||||
size_t const n = block_info_->pieceCount();
|
||||
auto pieces = tr_bitfield{ n };
|
||||
|
||||
// NOLINTNEXTLINE modernize-avoid-c-arrays
|
||||
auto flags = std::make_unique<bool[]>(n);
|
||||
for (tr_piece_index_t piece = 0; piece < n; ++piece)
|
||||
{
|
||||
|
||||
@@ -770,7 +770,7 @@ static bool isMartianAddr(struct tr_address const* a)
|
||||
{
|
||||
TR_ASSERT(tr_address_is_valid(a));
|
||||
|
||||
static unsigned char const zeroes[16] = {};
|
||||
static auto constexpr Zeroes = std::array<unsigned char, 16>{};
|
||||
|
||||
switch (a->type)
|
||||
{
|
||||
@@ -783,7 +783,8 @@ static bool isMartianAddr(struct tr_address const* a)
|
||||
case TR_AF_INET6:
|
||||
{
|
||||
auto const* const address = (unsigned char const*)&a->addr.addr6;
|
||||
return address[0] == 0xFF || (memcmp(address, zeroes, 15) == 0 && (address[15] == 0 || address[15] == 1));
|
||||
return address[0] == 0xFF ||
|
||||
(memcmp(address, std::data(Zeroes), 15) == 0 && (address[15] == 0 || address[15] == 1));
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
@@ -832,7 +832,7 @@ public:
|
||||
std::vector<tr_pex> pex;
|
||||
std::vector<tr_pex> pex6;
|
||||
|
||||
int peerAskedForMetadata[MetadataReqQ] = {};
|
||||
std::array<int, MetadataReqQ> peerAskedForMetadata = {};
|
||||
int peerAskedForMetadataCount = 0;
|
||||
|
||||
time_t clientSentAnythingAt = 0;
|
||||
@@ -1096,7 +1096,7 @@ static bool popNextMetadataRequest(tr_peerMsgsImpl* msgs, int* piece)
|
||||
|
||||
*piece = msgs->peerAskedForMetadata[0];
|
||||
|
||||
tr_removeElementFromArray(msgs->peerAskedForMetadata, 0, sizeof(int), msgs->peerAskedForMetadataCount);
|
||||
tr_removeElementFromArray(std::data(msgs->peerAskedForMetadata), 0, sizeof(int), msgs->peerAskedForMetadataCount);
|
||||
--msgs->peerAskedForMetadataCount;
|
||||
|
||||
return true;
|
||||
@@ -2294,7 +2294,7 @@ static size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
|
||||
if (msgs->isValidRequest(req) && msgs->torrent->hasPiece(req.index))
|
||||
{
|
||||
uint32_t const msglen = 4 + 1 + 4 + 4 + req.length;
|
||||
struct evbuffer_iovec iovec[1];
|
||||
struct evbuffer_iovec iovec = {};
|
||||
|
||||
auto* const out = evbuffer_new();
|
||||
evbuffer_expand(out, msglen);
|
||||
@@ -2304,14 +2304,14 @@ static size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
|
||||
evbuffer_add_uint32(out, req.index);
|
||||
evbuffer_add_uint32(out, req.offset);
|
||||
|
||||
evbuffer_reserve_space(out, req.length, iovec, 1);
|
||||
evbuffer_reserve_space(out, req.length, &iovec, 1);
|
||||
bool err = msgs->session->cache->readBlock(
|
||||
msgs->torrent,
|
||||
msgs->torrent->pieceLoc(req.index, req.offset),
|
||||
req.length,
|
||||
static_cast<uint8_t*>(iovec[0].iov_base)) != 0;
|
||||
iovec[0].iov_len = req.length;
|
||||
evbuffer_commit_space(out, iovec, 1);
|
||||
static_cast<uint8_t*>(iovec.iov_base)) != 0;
|
||||
iovec.iov_len = req.length;
|
||||
evbuffer_commit_space(out, &iovec, 1);
|
||||
|
||||
/* check the piece if it needs checking... */
|
||||
if (!err)
|
||||
|
||||
@@ -85,7 +85,7 @@ struct tr_rpc_address
|
||||
{
|
||||
struct in_addr addr4;
|
||||
struct in6_addr addr6;
|
||||
char unixSocketPath[TrUnixAddrStrLen];
|
||||
std::array<char, TrUnixAddrStrLen> unixSocketPath;
|
||||
} addr;
|
||||
|
||||
void set_inaddr_any()
|
||||
@@ -555,7 +555,7 @@ static char const* tr_rpc_address_to_string(tr_rpc_address const& addr, char* bu
|
||||
return evutil_inet_ntop(AF_INET6, &addr.addr, buf, buflen);
|
||||
|
||||
case TR_RPC_AF_UNIX:
|
||||
tr_strlcpy(buf, addr.addr.unixSocketPath, buflen);
|
||||
tr_strlcpy(buf, std::data(addr.addr.unixSocketPath), buflen);
|
||||
return buf;
|
||||
|
||||
default:
|
||||
@@ -590,7 +590,7 @@ static bool tr_rpc_address_from_string(tr_rpc_address& dst, std::string_view src
|
||||
}
|
||||
|
||||
dst.type = TR_RPC_AF_UNIX;
|
||||
tr_strlcpy(dst.addr.unixSocketPath, std::string{ src }.c_str(), TrUnixAddrStrLen);
|
||||
tr_strlcpy(std::data(dst.addr.unixSocketPath), std::string{ src }.c_str(), std::size(dst.addr.unixSocketPath));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// or any future license endorsed by Mnemosyne LLC.
|
||||
// License text can be found in the licenses/ folder.
|
||||
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <csignal>
|
||||
#include <map>
|
||||
@@ -139,9 +140,9 @@ bool tr_spawn_async(
|
||||
sigchld_handler_set = true;
|
||||
}
|
||||
|
||||
int pipe_fds[2];
|
||||
auto pipe_fds = std::array<int, 2>{};
|
||||
|
||||
if (pipe(pipe_fds) == -1)
|
||||
if (pipe(std::data(pipe_fds)) == -1)
|
||||
{
|
||||
set_system_error(error, errno, "Call to pipe()");
|
||||
return false;
|
||||
|
||||
@@ -355,18 +355,18 @@ void tr_dhtUninit(tr_session* ss)
|
||||
}
|
||||
else
|
||||
{
|
||||
auto constexpr MaxNodes = size_t{ 300 };
|
||||
auto constexpr MaxNodes = int{ 300 };
|
||||
auto constexpr PortLen = size_t{ 2 };
|
||||
auto constexpr CompactAddrLen = size_t{ 4 };
|
||||
auto constexpr CompactLen = size_t{ CompactAddrLen + PortLen };
|
||||
auto constexpr Compact6AddrLen = size_t{ 16 };
|
||||
auto constexpr Compact6Len = size_t{ Compact6AddrLen + PortLen };
|
||||
|
||||
struct sockaddr_in sins[MaxNodes];
|
||||
struct sockaddr_in6 sins6[MaxNodes];
|
||||
auto sins = std::array<struct sockaddr_in, MaxNodes>{};
|
||||
auto sins6 = std::array<struct sockaddr_in6, MaxNodes>{};
|
||||
int num = MaxNodes;
|
||||
int num6 = MaxNodes;
|
||||
int const n = dht_get_nodes(sins, &num, sins6, &num6);
|
||||
int const n = dht_get_nodes(std::data(sins), &num, std::data(sins6), &num6);
|
||||
tr_logAddTrace(fmt::format("Saving {} ({} + {}) nodes", n, num, num6));
|
||||
|
||||
tr_variant benc;
|
||||
@@ -375,9 +375,9 @@ void tr_dhtUninit(tr_session* ss)
|
||||
|
||||
if (num > 0)
|
||||
{
|
||||
char compact[MaxNodes * CompactLen];
|
||||
char* out = compact;
|
||||
for (struct sockaddr_in const* in = sins; in < sins + num; ++in)
|
||||
auto compact = std::array<char, MaxNodes * CompactLen>{};
|
||||
char* out = std::data(compact);
|
||||
for (auto const* in = std::data(sins), *end = in + num; in != end; ++in)
|
||||
{
|
||||
memcpy(out, &in->sin_addr, CompactAddrLen);
|
||||
out += CompactAddrLen;
|
||||
@@ -385,14 +385,14 @@ void tr_dhtUninit(tr_session* ss)
|
||||
out += PortLen;
|
||||
}
|
||||
|
||||
tr_variantDictAddRaw(&benc, TR_KEY_nodes, compact, out - compact);
|
||||
tr_variantDictAddRaw(&benc, TR_KEY_nodes, std::data(compact), out - std::data(compact));
|
||||
}
|
||||
|
||||
if (num6 > 0)
|
||||
{
|
||||
char compact6[MaxNodes * Compact6Len];
|
||||
char* out6 = compact6;
|
||||
for (struct sockaddr_in6 const* in = sins6; in < sins6 + num6; ++in)
|
||||
auto compact6 = std::array<char, MaxNodes * Compact6Len>{};
|
||||
char* out6 = std::data(compact6);
|
||||
for (auto const* in = std::data(sins6), *end = in + num6; in != end; ++in)
|
||||
{
|
||||
memcpy(out6, &in->sin6_addr, Compact6AddrLen);
|
||||
out6 += Compact6AddrLen;
|
||||
@@ -400,7 +400,7 @@ void tr_dhtUninit(tr_session* ss)
|
||||
out6 += PortLen;
|
||||
}
|
||||
|
||||
tr_variantDictAddRaw(&benc, TR_KEY_nodes6, compact6, out6 - compact6);
|
||||
tr_variantDictAddRaw(&benc, TR_KEY_nodes6, std::data(compact6), out6 - std::data(compact6));
|
||||
}
|
||||
|
||||
auto const dat_file = tr_pathbuf{ ss->configDir(), "/dht.dat"sv };
|
||||
|
||||
@@ -100,7 +100,7 @@ static void maxWidth(struct tr_option const* o, size_t& long_width, size_t& shor
|
||||
}
|
||||
}
|
||||
|
||||
void tr_getopt_usage(char const* appName, char const* description, struct tr_option const opts[])
|
||||
void tr_getopt_usage(char const* appName, char const* description, struct tr_option const* opts)
|
||||
{
|
||||
auto long_width = size_t{ 0 };
|
||||
auto short_width = size_t{ 0 };
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// or any future license endorsed by Mnemosyne LLC.
|
||||
// License text can be found in the licenses/ folder.
|
||||
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
@@ -41,7 +42,7 @@ enum class UpnpState
|
||||
WILL_UNMAP // next action is UPNP_DeletePortMapping()
|
||||
};
|
||||
|
||||
tr_port_forwarding portFwdState(UpnpState upnp_state, bool is_mapped)
|
||||
constexpr tr_port_forwarding portFwdState(UpnpState upnp_state, bool is_mapped)
|
||||
{
|
||||
switch (upnp_state)
|
||||
{
|
||||
@@ -87,7 +88,7 @@ struct tr_upnp
|
||||
UPNPUrls urls = {};
|
||||
IGDdatas data = {};
|
||||
tr_port port;
|
||||
char lanaddr[16] = {};
|
||||
std::string lanaddr;
|
||||
bool isMapped = false;
|
||||
UpnpState state = UpnpState::WILL_DISCOVER;
|
||||
|
||||
@@ -145,11 +146,8 @@ static struct UPNPDev* tr_upnpDiscover(int msec, char const* bindaddr)
|
||||
|
||||
static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto)
|
||||
{
|
||||
char intClient[16];
|
||||
char intPort[16];
|
||||
|
||||
*intClient = '\0';
|
||||
*intPort = '\0';
|
||||
auto int_client = std::array<char, 16>{};
|
||||
auto int_port = std::array<char, 16>{};
|
||||
|
||||
auto const port_str = fmt::format(FMT_STRING("{:d}"), handle->port.host());
|
||||
|
||||
@@ -160,8 +158,8 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto
|
||||
port_str.c_str(),
|
||||
proto,
|
||||
nullptr /*remoteHost*/,
|
||||
intClient,
|
||||
intPort,
|
||||
std::data(int_client),
|
||||
std::data(int_port),
|
||||
nullptr /*desc*/,
|
||||
nullptr /*enabled*/,
|
||||
nullptr /*duration*/);
|
||||
@@ -171,8 +169,8 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto
|
||||
handle->data.first.servicetype,
|
||||
port_str.c_str(),
|
||||
proto,
|
||||
intClient,
|
||||
intPort,
|
||||
std::data(int_client),
|
||||
std::data(int_port),
|
||||
nullptr /*desc*/,
|
||||
nullptr /*enabled*/,
|
||||
nullptr /*duration*/);
|
||||
@@ -182,8 +180,8 @@ static int tr_upnpGetSpecificPortMappingEntry(tr_upnp* handle, char const* proto
|
||||
handle->data.first.servicetype,
|
||||
port_str.c_str(),
|
||||
proto,
|
||||
intClient,
|
||||
intPort);
|
||||
std::data(int_client),
|
||||
std::data(int_port));
|
||||
#endif
|
||||
|
||||
return err;
|
||||
@@ -202,7 +200,7 @@ static int tr_upnpAddPortMapping(tr_upnp const* handle, char const* proto, tr_po
|
||||
handle->data.first.servicetype,
|
||||
port_str.c_str(),
|
||||
port_str.c_str(),
|
||||
handle->lanaddr,
|
||||
handle->lanaddr.c_str(),
|
||||
desc,
|
||||
proto,
|
||||
nullptr,
|
||||
@@ -213,7 +211,7 @@ static int tr_upnpAddPortMapping(tr_upnp const* handle, char const* proto, tr_po
|
||||
handle->data.first.servicetype,
|
||||
port_str.c_str(),
|
||||
port_str.c_str(),
|
||||
handle->lanaddr,
|
||||
handle->lanaddr.c_str(),
|
||||
desc,
|
||||
proto,
|
||||
nullptr);
|
||||
@@ -278,13 +276,15 @@ tr_port_forwarding tr_upnpPulse(tr_upnp* handle, tr_port port, bool isEnabled, b
|
||||
handle->discover_future.reset();
|
||||
|
||||
FreeUPNPUrls(&handle->urls);
|
||||
if (UPNP_GetValidIGD(devlist, &handle->urls, &handle->data, handle->lanaddr, sizeof(handle->lanaddr)) ==
|
||||
auto lanaddr = std::array<char, TR_ADDRSTRLEN>{};
|
||||
if (UPNP_GetValidIGD(devlist, &handle->urls, &handle->data, std::data(lanaddr), std::size(lanaddr)) ==
|
||||
UPNP_IGD_VALID_CONNECTED)
|
||||
{
|
||||
tr_logAddInfo(fmt::format(_("Found Internet Gateway Device '{url}'"), fmt::arg("url", handle->urls.controlURL)));
|
||||
tr_logAddInfo(fmt::format(_("Local Address is '{address}'"), fmt::arg("address", handle->lanaddr)));
|
||||
tr_logAddInfo(fmt::format(_("Local Address is '{address}'"), fmt::arg("address", std::data(handle->lanaddr))));
|
||||
handle->state = UpnpState::IDLE;
|
||||
handle->hasDiscovered = true;
|
||||
handle->lanaddr = std::data(lanaddr);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -736,7 +736,7 @@ uint64_t tr_htonll(uint64_t x)
|
||||
/* fallback code by bdonlan at https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */
|
||||
union
|
||||
{
|
||||
uint32_t lx[2];
|
||||
std::array<uint32_t, 2> lx;
|
||||
uint64_t llx;
|
||||
} u;
|
||||
u.lx[0] = htonl(x >> 32);
|
||||
@@ -757,7 +757,7 @@ uint64_t tr_ntohll(uint64_t x)
|
||||
/* fallback code by bdonlan at https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */
|
||||
union
|
||||
{
|
||||
uint32_t lx[2];
|
||||
std::array<uint32_t, 2> lx;
|
||||
uint64_t llx;
|
||||
} u;
|
||||
u.llx = x;
|
||||
|
||||
Reference in New Issue
Block a user