mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
Merge bitcoin/bitcoin#23411: refactor: Avoid integer overflow in ApplyStats when activating snapshot
fa996c58e8refactor: Avoid integer overflow in ApplyStats when activating snapshot (MarcoFalke)fac01888d1Move AdditionOverflow to util, Add CheckedAdd with unit tests (MarcoFalke)fa526d8fb6Add dev doc to CCoinsStats::m_hash_type and make it const (MarcoFalke)faff051560style: 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: reACKfa996c58e8vasild: ACKfa996c58e8Tree-SHA512: 4f207f634841f6f634fd02ae1e5907e343fd767524fd0e8149aa99fa9a1834fe50167d14874834d45236e9c325d567925f28129bacb7d80be29cf22277a16a14
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <util/getuniquepath.h>
|
||||
#include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
|
||||
#include <util/moneystr.h>
|
||||
#include <util/overflow.h>
|
||||
#include <util/spanparsing.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
@@ -1463,6 +1464,38 @@ BOOST_AUTO_TEST_CASE(test_IsDigit)
|
||||
BOOST_CHECK_EQUAL(IsDigit(9), false);
|
||||
}
|
||||
|
||||
/* Check for overflow */
|
||||
template <typename T>
|
||||
static void TestAddMatrixOverflow()
|
||||
{
|
||||
constexpr T MAXI{std::numeric_limits<T>::max()};
|
||||
BOOST_CHECK(!CheckedAdd(T{1}, MAXI));
|
||||
BOOST_CHECK(!CheckedAdd(MAXI, MAXI));
|
||||
BOOST_CHECK_EQUAL(0, CheckedAdd(T{0}, T{0}).value());
|
||||
BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{0}, MAXI).value());
|
||||
BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{1}, MAXI - 1).value());
|
||||
}
|
||||
|
||||
/* Check for overflow or underflow */
|
||||
template <typename T>
|
||||
static void TestAddMatrix()
|
||||
{
|
||||
TestAddMatrixOverflow<T>();
|
||||
constexpr T MINI{std::numeric_limits<T>::min()};
|
||||
constexpr T MAXI{std::numeric_limits<T>::max()};
|
||||
BOOST_CHECK(!CheckedAdd(T{-1}, MINI));
|
||||
BOOST_CHECK(!CheckedAdd(MINI, MINI));
|
||||
BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{0}, MINI).value());
|
||||
BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{-1}, MINI + 1).value());
|
||||
BOOST_CHECK_EQUAL(-1, CheckedAdd(MINI, MAXI).value());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_overflow)
|
||||
{
|
||||
TestAddMatrixOverflow<unsigned>();
|
||||
TestAddMatrix<signed>();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_ParseInt32)
|
||||
{
|
||||
int32_t n;
|
||||
|
||||
Reference in New Issue
Block a user