Merge bitcoin/bitcoin#36712: fees: prevent divide by zero in CFeeRate
What changed, and why it matters
This commit fixes a divide-by-zero crash in Bitcoin Core's fee-rate calculation. When a transaction fee rate was created with a size of zero, calling certain methods would try to divide by zero and crash the program. The fix makes these methods safely return zero instead, matching the behavior of related code, and adds tests to prevent the crash from coming back.
Apply the patch. Review other CFeeRate arithmetic and comparison paths for similar zero-size assumptions, and ensure all call sites that may construct a zero-size rate handle the empty case safely.
Security signals we found
Divide-by-zero crash in CFeeRate::GetFeePerK() and CFeeRate::operator+=
Zero-size fee rate construction reachable in normal fee computations
Patch adds explicit empty-rate guard and unit tests
Crash could lead to denial of service if triggered on a node code path
Evidence from the diff
The patch moves CFeeRate::GetFeePerK() from an inline header definition to a .cpp implementation and adds a guard: if m_feerate.IsEmpty(), it returns 0 instead of unconditionally calling EvaluateFeeDown(1000), which divides by the zero-size fee rate. It also adds unit tests covering zero-size construction (CFeeRate(0,0) and CFeeRate(5,0)) and the += operator on an empty rate. The crash is local and deterministic, not a remote code execution vector.
Changed components
src/policy/feerate.cppsrc/policy/feerate.hsrc/test/amount_tests.cppCFeeRate classInspect captured patch +14 / −1
### src/policy/feerate.cpp
@@ -18,6 +18,12 @@ CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)
}
}
+CAmount CFeeRate::GetFeePerK() const
+{
+ if (m_feerate.IsEmpty()) { return CAmount(0); }
+ return CAmount(m_feerate.EvaluateFeeDown(1000));
+}
+
CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
{
Assume(virtual_bytes >= 0);
### src/policy/feerate.h
@@ -68,7 +68,7 @@ class CFeeRate
/**
* Return the fee in satoshis for a vsize of 1000 vbytes
*/
- CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); }
+ CAmount GetFeePerK() const;
friend std::strong_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept
{
return ByRatio{a.m_feerate} <=> ByRatio{b.m_feerate};
### src/test/amount_tests.cpp
@@ -28,6 +28,8 @@ BOOST_AUTO_TEST_CASE(GetFeeTest)
// Must always return 0
BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
BOOST_CHECK_EQUAL(feeRate.GetFee(1e5), CAmount(0));
+ BOOST_CHECK_EQUAL(CFeeRate(0, 0).GetFeePerK(), CAmount{0});
+ BOOST_CHECK_EQUAL(CFeeRate(CAmount{5}, 0).GetFeePerK(), CAmount{0});
feeRate = CFeeRate(1000);
// Must always just return the arg
@@ -93,6 +95,11 @@ BOOST_AUTO_TEST_CASE(GetFeeTest)
// Maximum size in bytes, should not crash
CFeeRate(MAX_MONEY, std::numeric_limits<int32_t>::max()).GetFeePerK();
+ // Adding to a zero size fee rate should not crash
+ CFeeRate empty_rate(0, 0);
+ empty_rate += CFeeRate(1, 1);
+ BOOST_CHECK(empty_rate == CFeeRate(1, 1));
+
// check multiplication operator
// check multiplying by zero
feeRate = CFeeRate(1000);Why this scored 60/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.