mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-07-26 16:18:47 +02:00
Merge bitcoin/bitcoin#35372: refactor: Enhance type safety in overflow operations
0774eaaf0cutil: Require integers for SaturatingAdd() and AdditionOverflow() (Hodlinator)a815e3e262rpc: Correct type for tx_sigops (Hodlinator) Pull request description: * Correct copy-paste error in RPC code * Require proper integers for `SaturatingAdd()` and `AdditionOverflow()` in src/util/overflow.h These changes increase the type safety of the code and were done while exploring increasing the type-safety of `CAmount` (currently just a `typedef` of `int64_t`). The first commit has nothing to do with overflow but is along for the ride if reviewers agree. ACKs for top commit: maflcko: lgtm ACK0774eaaf0cwinterrdog: ACK0774eaaf0csedited: ACK0774eaaf0cTree-SHA512: a245ad52cfd1c257151aea1a1ed4b6769415c1dddc7c405d8bbe71b9f3abc512a6d890a45cbc8381718be16274e337cc876e6e6da11dc35de71bea83bece6634
This commit is contained in:
@@ -927,7 +927,7 @@ static RPCMethod getblocktemplate()
|
||||
UniValue transactions(UniValue::VARR);
|
||||
std::map<Txid, int64_t> setTxIndex;
|
||||
std::vector<CAmount> tx_fees{block_template->getTxFees()};
|
||||
std::vector<CAmount> tx_sigops{block_template->getTxSigops()};
|
||||
std::vector<int64_t> tx_sigops{block_template->getTxSigops()};
|
||||
|
||||
int i = 0;
|
||||
for (const auto& it : block.vtx) {
|
||||
|
||||
@@ -13,10 +13,9 @@
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
|
||||
template <class T>
|
||||
template <std::integral T>
|
||||
[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept
|
||||
{
|
||||
static_assert(std::is_integral_v<T>, "Integral required.");
|
||||
if constexpr (std::numeric_limits<T>::is_signed) {
|
||||
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
|
||||
(i < 0 && j < std::numeric_limits<T>::min() - i);
|
||||
@@ -41,7 +40,7 @@ template <std::unsigned_integral T, std::unsigned_integral U>
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
template <std::integral T>
|
||||
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept
|
||||
{
|
||||
if constexpr (std::numeric_limits<T>::is_signed) {
|
||||
|
||||
Reference in New Issue
Block a user