From 19543ba65fe4ea371a15fe956ac0850fb549265c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 21 Oct 2024 20:57:01 -0500 Subject: [PATCH] fix: clang-tidy-20 warnings (#7187) * fix: readability-math-missing-parentheses clang-tidy warnings * chore: remove unused function tr_ctorGetSession() * chore: remove unused function tr_ctorGetIncompleteDir() * chore: make generatePublicKey() a lambda * fix: readability-container-contains warnings * fix: misc-use-internal-linkage warnings * chore: inline generate_public_key() since it was only used once --- libtransmission/announcer.cc | 10 +++++----- libtransmission/bandwidth.cc | 2 +- libtransmission/bitfield.cc | 2 +- libtransmission/crypto-utils.cc | 4 ++-- libtransmission/handshake.cc | 2 +- libtransmission/peer-mgr-active-requests.cc | 4 ++-- libtransmission/peer-mgr.cc | 2 +- libtransmission/peer-mse.cc | 11 +++-------- libtransmission/session-alt-speeds.cc | 2 +- libtransmission/session.cc | 2 +- libtransmission/torrent-ctor.cc | 20 -------------------- 11 files changed, 18 insertions(+), 43 deletions(-) diff --git a/libtransmission/announcer.cc b/libtransmission/announcer.cc index 6cfbbe7b5..6e40995b5 100644 --- a/libtransmission/announcer.cc +++ b/libtransmission/announcer.cc @@ -258,19 +258,19 @@ struct tr_tracker return 20U; case 2: - return tr_rand_int(60U) + 60U * 5U; + return tr_rand_int(60U) + (60U * 5U); case 3: - return tr_rand_int(60U) + 60U * 15U; + return tr_rand_int(60U) + (60U * 15U); case 4: - return tr_rand_int(60U) + 60U * 30U; + return tr_rand_int(60U) + (60U * 30U); case 5: - return tr_rand_int(60U) + 60U * 60U; + return tr_rand_int(60U) + (60U * 60U); default: - return tr_rand_int(60U) + 60U * 120U; + return tr_rand_int(60U) + (60U * 120U); } } diff --git a/libtransmission/bandwidth.cc b/libtransmission/bandwidth.cc index 12f020a2a..d76497c0e 100644 --- a/libtransmission/bandwidth.cc +++ b/libtransmission/bandwidth.cc @@ -182,7 +182,7 @@ void tr_bandwidth::phase_one(std::vector& peers, tr_direction dir) tr_logAddTrace(fmt::format("{} peers to go round-robin for {}", peers.size(), dir == TR_UP ? "upload" : "download")); // Shuffle the peers so they all have equal chance to be first in line. - thread_local auto urbg = tr_urbg{}; + static thread_local auto urbg = tr_urbg{}; std::shuffle(std::begin(peers), std::end(peers), urbg); // Give each peer `Increment` bandwidth bytes to use. Repeat this diff --git a/libtransmission/bitfield.cc b/libtransmission/bitfield.cc index f0d99b705..a114c5ce2 100644 --- a/libtransmission/bitfield.cc +++ b/libtransmission/bitfield.cc @@ -292,7 +292,7 @@ void tr_bitfield::set_raw(uint8_t const* raw, size_t byte_count) // ensure any excess bits at the end of the array are set to '0'. if (byte_count == getBytesNeededSafe(bit_count_)) { - auto const excess_bit_count = byte_count * 8 - bit_count_; + auto const excess_bit_count = (byte_count * 8) - bit_count_; TR_ASSERT(excess_bit_count <= 7); diff --git a/libtransmission/crypto-utils.cc b/libtransmission/crypto-utils.cc index a1adf129d..2ef9fa04f 100644 --- a/libtransmission/crypto-utils.cc +++ b/libtransmission/crypto-utils.cc @@ -242,8 +242,8 @@ std::optional tr_sha256_from_string(std::string_view hex) // fallback implementation in case the system crypto library's RNG fails void tr_rand_buffer_std(void* buffer, size_t length) { - thread_local auto gen = std::mt19937{ std::random_device{}() }; - thread_local auto dist = std::uniform_int_distribution{}; + static thread_local auto gen = std::mt19937{ std::random_device{}() }; + static thread_local auto dist = std::uniform_int_distribution{}; for (auto *walk = static_cast(buffer), *end = walk + length; walk < end;) { diff --git a/libtransmission/handshake.cc b/libtransmission/handshake.cc index 959d1b14a..0bc8e9ceb 100644 --- a/libtransmission/handshake.cc +++ b/libtransmission/handshake.cc @@ -77,7 +77,7 @@ ReadState tr_handshake::read_yb(tr_peerIo* peer_io) /* now send these: HASH('req1', S), HASH('req2', SKEY) xor HASH('req3', S), * ENCRYPT(VC, crypto_provide, len(PadC), PadC, len(IA)), ENCRYPT(IA) */ - static auto constexpr BufSize = std::tuple_size_v * 2 + std::size(VC) + sizeof(crypto_provide_) + + static auto constexpr BufSize = (std::tuple_size_v * 2U) + std::size(VC) + sizeof(crypto_provide_) + sizeof(pad_c_len_) + sizeof(ia_len_) + HandshakeSize; auto outbuf = libtransmission::StackBuffer{}; diff --git a/libtransmission/peer-mgr-active-requests.cc b/libtransmission/peer-mgr-active-requests.cc index 506b598fb..5662136b9 100644 --- a/libtransmission/peer-mgr-active-requests.cc +++ b/libtransmission/peer-mgr-active-requests.cc @@ -114,7 +114,7 @@ std::vector ActiveRequests::remove(tr_peer const* peer) for (auto const& [block, peers_at] : impl_->blocks_) { - if (peers_at.count(peer) != 0U) + if (peers_at.contains(peer)) { removed.push_back(block); } @@ -157,7 +157,7 @@ std::vector ActiveRequests::remove(tr_block_index_t block) bool ActiveRequests::has(tr_block_index_t block, tr_peer const* peer) const { auto const iter = impl_->blocks_.find(block); - return iter != std::end(impl_->blocks_) && (iter->second.count(peer) != 0U); + return iter != std::end(impl_->blocks_) && iter->second.contains(peer); } // count how many peers we're asking for `block` diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index df89de0f2..6061a4f2f 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -2129,7 +2129,7 @@ auto constexpr MaxUploadIdleSecs = time_t{ 60 * 5 }; peer_count / (float)relax_strictness_if_fewer_than_n; auto const lo = MinUploadIdleSecs; auto const hi = MaxUploadIdleSecs; - time_t const limit = hi - (hi - lo) * strictness; + time_t const limit = hi - ((hi - lo) * strictness); if (auto const idle_secs = info->idle_secs(now); idle_secs && *idle_secs > limit) { diff --git a/libtransmission/peer-mse.cc b/libtransmission/peer-mse.cc index 865ab61b6..de7a8a096 100644 --- a/libtransmission/peer-mse.cc +++ b/libtransmission/peer-mse.cc @@ -78,18 +78,13 @@ namespace tr_message_stream_encryption return tr_rand_obj(); } -[[nodiscard]] auto generatePublicKey(DH::private_key_bigend_t const& private_key) noexcept -{ - auto const private_key_wi = wi::import_bits(private_key); - auto const public_key_wi = math::wide_integer::powm(wi::generator, private_key_wi, wi::prime); - return wi::export_bits(public_key_wi); -} - DH::key_bigend_t DH::publicKey() noexcept { if (public_key_ == key_bigend_t{}) { - public_key_ = generatePublicKey(private_key_); + auto const private_key_wi = wi::import_bits(private_key_); + auto const public_key_wi = math::wide_integer::powm(wi::generator, private_key_wi, wi::prime); + public_key_ = wi::export_bits(public_key_wi); } return public_key_; diff --git a/libtransmission/session-alt-speeds.cc b/libtransmission/session-alt-speeds.cc index 47180d4c4..a15d153ed 100644 --- a/libtransmission/session-alt-speeds.cc +++ b/libtransmission/session-alt-speeds.cc @@ -80,7 +80,7 @@ void tr_session_alt_speeds::set_active(bool active, ChangeReason reason, bool fo { auto const tm = fmt::localtime(time); - size_t minute_of_the_week = tm.tm_wday * MinutesPerDay + tm.tm_hour * MinutesPerHour + tm.tm_min; + size_t minute_of_the_week = (tm.tm_wday * MinutesPerDay) + (tm.tm_hour * MinutesPerHour) + tm.tm_min; if (minute_of_the_week >= MinutesPerWeek) /* leap minutes? */ { diff --git a/libtransmission/session.cc b/libtransmission/session.cc index 77d3add97..2bd1f22ac 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -200,7 +200,7 @@ tr_peer_id_t tr_peerIdInit() total += val; *it++ = Pool[val]; } - int const val = total % std::size(Pool) != 0 ? std::size(Pool) - total % std::size(Pool) : 0; + int const val = total % std::size(Pool) != 0 ? std::size(Pool) - (total % std::size(Pool)) : 0; *it = Pool[val]; return peer_id; diff --git a/libtransmission/torrent-ctor.cc b/libtransmission/torrent-ctor.cc index 87b86bfe8..b801eee27 100644 --- a/libtransmission/torrent-ctor.cc +++ b/libtransmission/torrent-ctor.cc @@ -84,11 +84,6 @@ void tr_ctorFree(tr_ctor* ctor) delete ctor; } -tr_session* tr_ctorGetSession(tr_ctor const* ctor) -{ - return ctor->session(); -} - bool tr_ctorSetMetainfoFromFile(tr_ctor* const ctor, char const* const filename, tr_error* const error) { return ctor->set_metainfo_from_file(std::string_view{ filename != nullptr ? filename : "" }, error); @@ -210,21 +205,6 @@ bool tr_ctorGetDownloadDir(tr_ctor const* const ctor, tr_ctorMode const mode, ch return false; } -bool tr_ctorGetIncompleteDir(tr_ctor const* const ctor, char const** setme) -{ - if (auto const& val = ctor->incomplete_dir(); !std::empty(val)) - { - if (setme != nullptr) - { - *setme = val.c_str(); - } - - return true; - } - - return false; -} - tr_torrent_metainfo const* tr_ctorGetMetainfo(tr_ctor const* const ctor) { auto const& metainfo = ctor->metainfo();