From 2b90a5fd55e2eff72f2d41668b39bfae56144f40 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 29 Dec 2022 10:30:03 -0600 Subject: [PATCH] refactor: possible FTBFS fix on arm7 (#4492) * refactor: possible FTBFS fix on arm7 * chore: remove tr_rand_int_weak() from crypto-test-ref * refactor: move tr_rand_int() to header --- libtransmission/crypto-utils.cc | 17 ----------------- libtransmission/crypto-utils.h | 21 +++++++++++++++------ tests/libtransmission/crypto-test-ref.h | 3 --- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/libtransmission/crypto-utils.cc b/libtransmission/crypto-utils.cc index 7313c7e08..2753335bd 100644 --- a/libtransmission/crypto-utils.cc +++ b/libtransmission/crypto-utils.cc @@ -31,23 +31,6 @@ using namespace std::literals; /// -template -[[nodiscard]] T tr_rand_int(T upper_bound) -{ - static_assert(!std::is_signed()); - TR_ASSERT(upper_bound > std::numeric_limits::min()); - - using dist_type = std::uniform_int_distribution; - thread_local auto rng = tr_urbg{}; - thread_local auto dist = dist_type{}; - return dist(rng, typename dist_type::param_type(0, upper_bound - 1)); -} - -template size_t tr_rand_int(size_t upper_bound); -template unsigned int tr_rand_int(unsigned int upper_bound); - -/// - namespace { namespace ssha1_impl diff --git a/libtransmission/crypto-utils.h b/libtransmission/crypto-utils.h index 3a889463b..510f8eac4 100644 --- a/libtransmission/crypto-utils.h +++ b/libtransmission/crypto-utils.h @@ -12,6 +12,7 @@ #include #include #include +#include // for std::uniform_int_distribution #include #include @@ -87,12 +88,6 @@ tr_x509_cert_t tr_x509_cert_new(void const* der, size_t der_length); */ void tr_x509_cert_free(tr_x509_cert_t handle); -/** - * @brief Returns a random number in the range of [0...upper_bound). - */ -template -[[nodiscard]] IntType tr_rand_int(IntType upper_bound); - /** * @brief Fill a buffer with random bytes. */ @@ -212,6 +207,20 @@ private: tr_salt_shaker buf_; }; +/** + * @brief Returns a random number in the range of [0...upper_bound). + */ +template +[[nodiscard]] T tr_rand_int(T upper_bound) +{ + static_assert(!std::is_signed()); + using dist_type = std::uniform_int_distribution; + + thread_local auto rng = tr_urbg{}; + thread_local auto dist = dist_type{}; + return dist(rng, typename dist_type::param_type(0, upper_bound - 1)); +} + /** @} */ #endif /* TR_CRYPTO_UTILS_H */ diff --git a/tests/libtransmission/crypto-test-ref.h b/tests/libtransmission/crypto-test-ref.h index 67da1fa56..7be0d3465 100644 --- a/tests/libtransmission/crypto-test-ref.h +++ b/tests/libtransmission/crypto-test-ref.h @@ -18,7 +18,6 @@ #define tr_rand_buffer_crypto tr_rand_buffer_crypto_ #define tr_rand_buffer_std tr_rand_buffer_std_ #define tr_rand_int tr_rand_int_ -#define tr_rand_int_weak tr_rand_int_weak_ #define tr_rand_obj tr_rand_obj_ #define tr_salt_shaker tr_salt_shaker_ #define tr_sha1 tr_sha1_ @@ -54,7 +53,6 @@ #undef tr_rand_buffer_crypto #undef tr_rand_buffer_std #undef tr_rand_int -#undef tr_rand_int_weak #undef tr_rand_obj #undef tr_salt_shaker #undef tr_sha1 @@ -85,7 +83,6 @@ #define tr_rand_buffer_crypto_ tr_rand_buffer_crypto #define tr_rand_buffer_std_ tr_rand_buffer_std #define tr_rand_int_ tr_rand_int -#define tr_rand_int_weak_ tr_rand_int_weak #define tr_rand_obj_ tr_rand_obj #define tr_salt_shaker_ tr_salt_shaker #define tr_sha1_ tr_sha1