diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-11-01 17:03:09 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-12-17 10:47:31 +0100 |
commit | fa996c58e8a31ebe610d186cef408b6dd3b385a8 (patch) | |
tree | db5a678ba2b97fa080446d10247a92c9cc426895 /src/node | |
parent | fac01888d17423d6c23a9ce15d98fc88fb34e3cc (diff) |
refactor: Avoid integer overflow in ApplyStats when activating snapshot
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/coinstats.cpp | 5 | ||||
-rw-r--r-- | src/node/coinstats.h | 3 |
2 files changed, 6 insertions, 2 deletions
diff --git a/src/node/coinstats.cpp b/src/node/coinstats.cpp index 639a579172..eb21bbdba8 100644 --- a/src/node/coinstats.cpp +++ b/src/node/coinstats.cpp @@ -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); } } diff --git a/src/node/coinstats.h b/src/node/coinstats.h index 1d95417d4f..2a8fbf5bc6 100644 --- a/src/node/coinstats.h +++ b/src/node/coinstats.h @@ -35,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}; |