mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-15 23:54:17 +02:00
Merge bitcoin/bitcoin#23411: refactor: Avoid integer overflow in ApplyStats when activating snapshot
fa996c58e8
refactor: Avoid integer overflow in ApplyStats when activating snapshot (MarcoFalke)fac01888d1
Move AdditionOverflow to util, Add CheckedAdd with unit tests (MarcoFalke)fa526d8fb6
Add dev doc to CCoinsStats::m_hash_type and make it const (MarcoFalke)faff051560
style: Remove unused whitespace (MarcoFalke) Pull request description: A snapshot contains the utxo set, including the out value. To activate the snapshot, the hash needs to be calculated. As a side-effect, the total amount in the snapshot is calculated (as the sum of all out values), but never used. Instead of running into an integer overflow in an unused result, don't calculate the result in the first place. Other code paths (using the active utxo set) can not run into an integer overflow, since the active utxo set is valid. Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=39716 ACKs for top commit: shaavan: reACKfa996c58e8
vasild: ACKfa996c58e8
Tree-SHA512: 4f207f634841f6f634fd02ae1e5907e343fd767524fd0e8149aa99fa9a1834fe50167d14874834d45236e9c325d567925f28129bacb7d80be29cf22277a16a14
This commit is contained in:
@ -11,6 +11,7 @@
|
||||
#include <index/coinstatsindex.h>
|
||||
#include <serialize.h>
|
||||
#include <uint256.h>
|
||||
#include <util/overflow.h>
|
||||
#include <util/system.h>
|
||||
#include <validation.h>
|
||||
|
||||
@ -82,7 +83,9 @@ static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<u
|
||||
stats.nTransactions++;
|
||||
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
||||
stats.nTransactionOutputs++;
|
||||
stats.nTotalAmount += it->second.out.nValue;
|
||||
if (stats.total_amount.has_value()) {
|
||||
stats.total_amount = CheckedAdd(*stats.total_amount, it->second.out.nValue);
|
||||
}
|
||||
stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
|
||||
}
|
||||
}
|
||||
@ -95,10 +98,8 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
|
||||
assert(pcursor);
|
||||
|
||||
if (!pindex) {
|
||||
{
|
||||
LOCK(cs_main);
|
||||
pindex = blockman.LookupBlockIndex(view->GetBestBlock());
|
||||
}
|
||||
LOCK(cs_main);
|
||||
pindex = blockman.LookupBlockIndex(view->GetBestBlock());
|
||||
}
|
||||
stats.nHeight = Assert(pindex)->nHeight;
|
||||
stats.hashBlock = pindex->GetBlockHash();
|
||||
|
@ -24,9 +24,10 @@ enum class CoinStatsHashType {
|
||||
NONE,
|
||||
};
|
||||
|
||||
struct CCoinsStats
|
||||
{
|
||||
CoinStatsHashType m_hash_type;
|
||||
struct CCoinsStats {
|
||||
//! Which hash type to use
|
||||
const CoinStatsHashType m_hash_type;
|
||||
|
||||
int nHeight{0};
|
||||
uint256 hashBlock{};
|
||||
uint64_t nTransactions{0};
|
||||
@ -34,7 +35,8 @@ struct CCoinsStats
|
||||
uint64_t nBogoSize{0};
|
||||
uint256 hashSerialized{};
|
||||
uint64_t nDiskSize{0};
|
||||
CAmount nTotalAmount{0};
|
||||
//! The total amount, or nullopt if an overflow occurred calculating it
|
||||
std::optional<CAmount> total_amount{0};
|
||||
|
||||
//! The number of coins contained.
|
||||
uint64_t coins_count{0};
|
||||
|
Reference in New Issue
Block a user