Skip to content

Commit

Permalink
Merge pull request reddcoin-project#267 from reddink/fix_overflow_max…
Browse files Browse the repository at this point in the history
…money

Fix overflow maxmoney
  • Loading branch information
reddink authored Dec 20, 2023
2 parents c263d78 + f9b6046 commit c348a0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/policy/feerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <arith_uint256.h>
#include <policy/feerate.h>

#include <tinyformat.h>
Expand All @@ -14,7 +15,10 @@ CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes)
{
const int64_t nSize{num_bytes};

if (nSize > 0) {
if (nFeePaid >= MAX_MONEY && nSize > 0) {
arith_uint256 nSatsPerK = arith_uint256(nFeePaid) * 1000 / arith_uint256(nSize);
nSatoshisPerK = nSatsPerK.GetLow64();
} else if (nSize > 0) {
nSatoshisPerK = nFeePaid * 1000 / nSize;
} else {
nSatoshisPerK = 0;
Expand Down
11 changes: 10 additions & 1 deletion src/test/amount_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <amount.h>
#include <policy/feerate.h>

#include <test/util/setup_common.h>

#include <limits>

#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -84,8 +86,15 @@ BOOST_AUTO_TEST_CASE(GetFeeTest)
// some more integer checks
BOOST_CHECK(CFeeRate(CAmount(26), 789) == CFeeRate(32));
BOOST_CHECK(CFeeRate(CAmount(27), 789) == CFeeRate(34));
}

BOOST_AUTO_TEST_CASE(SizeLimitTest)
{
// Maximum size in bytes, should not crash
CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).GetFeePerK();
BOOST_CHECK(CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()) == CFeeRate(2147483648487));
BOOST_CHECK_EQUAL(CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).ToString() , "21474.83648487 RDD/kvB");
BOOST_CHECK_EQUAL(CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).ToString(FeeEstimateMode::BTC_KVB) , "21474.83648487 RDD/kvB");
BOOST_CHECK_EQUAL(CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).ToString(FeeEstimateMode::SAT_VB) , "2147483648.487 sat/vB");
}

BOOST_AUTO_TEST_CASE(BinaryOperatorTest)
Expand Down

0 comments on commit c348a0a

Please sign in to comment.