Skip to content

Commit 0882406

Browse files
committed
Merge #13437: wallet: Erase wtxOrderd wtx pointer on removeprunedfunds
faa18ca wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke) Pull request description: This prevents segfaults, when reading from the freed memory. Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123
2 parents ac3224c + faa18ca commit 0882406

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,10 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
923923
CWalletTx& wtx = (*ret.first).second;
924924
wtx.BindWallet(this);
925925
bool fInsertedNew = ret.second;
926-
if (fInsertedNew)
927-
{
926+
if (fInsertedNew) {
928927
wtx.nTimeReceived = GetAdjustedTime();
929928
wtx.nOrderPos = IncOrderPosNext(&batch);
930-
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
929+
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
931930
wtx.nTimeSmart = ComputeTimeSmart(wtx);
932931
AddToSpends(hash);
933932
}
@@ -998,9 +997,12 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
998997
bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
999998
{
1000999
uint256 hash = wtxIn.GetHash();
1001-
CWalletTx& wtx = mapWallet.emplace(hash, wtxIn).first->second;
1000+
const auto& ins = mapWallet.emplace(hash, wtxIn);
1001+
CWalletTx& wtx = ins.first->second;
10021002
wtx.BindWallet(this);
1003-
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
1003+
if (/* insertion took place */ ins.second) {
1004+
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
1005+
}
10041006
AddToSpends(hash);
10051007
for (const CTxIn& txin : wtx.tx->vin) {
10061008
auto it = mapWallet.find(txin.prevout.hash);
@@ -3210,8 +3212,11 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
32103212
{
32113213
AssertLockHeld(cs_wallet); // mapWallet
32123214
DBErrors nZapSelectTxRet = WalletBatch(*database,"cr+").ZapSelectTx(vHashIn, vHashOut);
3213-
for (uint256 hash : vHashOut)
3214-
mapWallet.erase(hash);
3215+
for (uint256 hash : vHashOut) {
3216+
const auto& it = mapWallet.find(hash);
3217+
wtxOrdered.erase(it->second.m_it_wtxOrdered);
3218+
mapWallet.erase(it);
3219+
}
32153220

32163221
if (nZapSelectTxRet == DBErrors::NEED_REWRITE)
32173222
{

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ class CWalletTx : public CMerkleTx
339339
char fFromMe;
340340
std::string strFromAccount;
341341
int64_t nOrderPos; //!< position in ordered transaction list
342+
std::multimap<int64_t, std::pair<CWalletTx*, CAccountingEntry*>>::const_iterator m_it_wtxOrdered;
342343

343344
// memory only
344345
mutable bool fDebitCached;

0 commit comments

Comments
 (0)