Skip to content

Commit d020546

Browse files
authored
Merge pull request ElementsProject#1401 from apoelstra/2025-02--fuzz-fixes-2
more fuzzer fixes (continuation of ElementsProject#1399)
2 parents d7efd31 + 56eec70 commit d020546

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/txmempool.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include <policy/policy.h>
1919
#include <policy/settings.h>
2020
#include <reverse_iterator.h>
21+
#include <util/check.h>
2122
#include <util/moneystr.h>
23+
#include <util/overflow.h>
2224
#include <util/system.h>
2325
#include <util/time.h>
2426
#include <validationinterface.h>
@@ -89,6 +91,7 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,
8991
entryHeight{entry_height},
9092
spendsCoinbase{spends_coinbase},
9193
sigOpCost{sigops_cost},
94+
m_modified_fee{nFee},
9295
lockPoints{lp},
9396
nSizeWithDescendants{GetTxSize()},
9497
nModFeesWithDescendants{nFee},
@@ -98,11 +101,11 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,
98101
discountSizeWithAncestors{GetDiscountTxSize()},
99102
setPeginsSpent(_setPeginsSpent) {}
100103

101-
void CTxMemPoolEntry::UpdateFeeDelta(CAmount newFeeDelta)
104+
void CTxMemPoolEntry::UpdateModifiedFee(CAmount fee_diff)
102105
{
103-
nModFeesWithDescendants += newFeeDelta - feeDelta;
104-
nModFeesWithAncestors += newFeeDelta - feeDelta;
105-
feeDelta = newFeeDelta;
106+
nModFeesWithDescendants = SaturatingAdd(nModFeesWithDescendants, fee_diff);
107+
nModFeesWithAncestors = SaturatingAdd(nModFeesWithAncestors, fee_diff);
108+
m_modified_fee = SaturatingAdd(m_modified_fee, fee_diff);
106109
}
107110

108111
void CTxMemPoolEntry::UpdateLockPoints(const LockPoints& lp)
@@ -457,7 +460,7 @@ void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize, CAmount modifyFe
457460
{
458461
nSizeWithDescendants += modifySize;
459462
assert(int64_t(nSizeWithDescendants) > 0);
460-
nModFeesWithDescendants += modifyFee;
463+
nModFeesWithDescendants = SaturatingAdd(nModFeesWithDescendants, modifyFee);
461464
nCountWithDescendants += modifyCount;
462465
assert(int64_t(nCountWithDescendants) > 0);
463466
}
@@ -466,7 +469,7 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee,
466469
{
467470
nSizeWithAncestors += modifySize;
468471
assert(int64_t(nSizeWithAncestors) > 0);
469-
nModFeesWithAncestors += modifyFee;
472+
nModFeesWithAncestors = SaturatingAdd(nModFeesWithAncestors, modifyFee);
470473
nCountWithAncestors += modifyCount;
471474
assert(int64_t(nCountWithAncestors) > 0);
472475
nSigOpCostWithAncestors += modifySigOps;
@@ -509,8 +512,10 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
509512
// into mapTx.
510513
CAmount delta{0};
511514
ApplyDelta(entry.GetTx().GetHash(), delta);
515+
// The following call to UpdateModifiedFee assumes no previous fee modifications
516+
Assume(entry.GetFee() == entry.GetModifiedFee());
512517
if (delta) {
513-
mapTx.modify(newit, [&delta](CTxMemPoolEntry& e) { e.UpdateFeeDelta(delta); });
518+
mapTx.modify(newit, [&delta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(delta); });
514519
}
515520

516521
// Update cachedInnerUsage to include contained transaction's usage.
@@ -1014,10 +1019,10 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
10141019
{
10151020
LOCK(cs);
10161021
CAmount &delta = mapDeltas[hash];
1017-
delta += nFeeDelta;
1022+
delta = SaturatingAdd(delta, nFeeDelta);
10181023
txiter it = mapTx.find(hash);
10191024
if (it != mapTx.end()) {
1020-
mapTx.modify(it, [&delta](CTxMemPoolEntry& e) { e.UpdateFeeDelta(delta); });
1025+
mapTx.modify(it, [&nFeeDelta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(nFeeDelta); });
10211026
// Now update all ancestors' modified fees with descendants
10221027
setEntries setAncestors;
10231028
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();

src/txmempool.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class CTxMemPoolEntry
102102
const unsigned int entryHeight; //!< Chain height when entering the mempool
103103
const bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
104104
const int64_t sigOpCost; //!< Total sigop cost
105-
CAmount feeDelta{0}; //!< Used for determining the priority of the transaction for mining in a block
105+
CAmount m_modified_fee; //!< Used for determining the priority of the transaction for mining in a block
106106
LockPoints lockPoints; //!< Track the height and time at which tx was final
107107

108108
// Information about descendants of this transaction that are in the
@@ -135,17 +135,16 @@ class CTxMemPoolEntry
135135
std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
136136
unsigned int GetHeight() const { return entryHeight; }
137137
int64_t GetSigOpCost() const { return sigOpCost; }
138-
CAmount GetModifiedFee() const { return nFee + feeDelta; }
138+
CAmount GetModifiedFee() const { return m_modified_fee; }
139139
size_t DynamicMemoryUsage() const { return nUsageSize; }
140140
const LockPoints& GetLockPoints() const { return lockPoints; }
141141

142142
// Adjusts the descendant state.
143143
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
144144
// Adjusts the ancestor state
145145
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps, int64_t discountSize);
146-
// Updates the fee delta used for mining priority score, and the
147-
// modified fees with descendants/ancestors.
148-
void UpdateFeeDelta(CAmount newFeeDelta);
146+
// Updates the modified fees with descendants/ancestors.
147+
void UpdateModifiedFee(CAmount fee_diff);
149148
// Update the LockPoints after a reorg
150149
void UpdateLockPoints(const LockPoints& lp);
151150

test/sanitizer_suppressions/ubsan

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -fsanitize=undefined suppressions
22
# =================================
3-
# This would be `signed-integer-overflow:CTxMemPool::PrioritiseTransaction`,
3+
# The suppressions would be `sanitize-type:ClassName::MethodName`,
44
# however due to a bug in clang the symbolizer is disabled and thus no symbol
55
# names can be used.
66
# See https://github.com/google/sanitizers/issues/1364
7-
signed-integer-overflow:txmempool.cpp
7+
88
# https://github.com/bitcoin/bitcoin/pull/21798#issuecomment-829180719
99
signed-integer-overflow:policy/feerate.cpp
1010

0 commit comments

Comments
 (0)