Skip to content

Commit 23704da

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent 15fb6db commit 23704da

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
@@ -1063,11 +1063,10 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
10631063
CWalletTx& wtx = (*ret.first).second;
10641064
wtx.BindWallet(this);
10651065
bool fInsertedNew = ret.second;
1066-
if (fInsertedNew)
1067-
{
1066+
if (fInsertedNew) {
10681067
wtx.nTimeReceived = GetAdjustedTime();
10691068
wtx.nOrderPos = IncOrderPosNext(&walletdb);
1070-
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
1069+
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
10711070
wtx.nTimeSmart = ComputeTimeSmart(wtx);
10721071
AddToSpends(hash);
10731072

@@ -1141,9 +1140,12 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
11411140
bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
11421141
{
11431142
uint256 hash = wtxIn.GetHash();
1144-
CWalletTx& wtx = mapWallet.emplace(hash, wtxIn).first->second;
1143+
const auto& ins = mapWallet.emplace(hash, wtxIn);
1144+
CWalletTx& wtx = ins.first->second;
11451145
wtx.BindWallet(this);
1146-
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
1146+
if (/* insertion took place */ ins.second) {
1147+
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
1148+
}
11471149
AddToSpends(hash);
11481150
for (const CTxIn& txin : wtx.tx->vin) {
11491151
auto it = mapWallet.find(txin.prevout.hash);
@@ -4172,8 +4174,11 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
41724174
{
41734175
AssertLockHeld(cs_wallet); // mapWallet
41744176
DBErrors nZapSelectTxRet = CWalletDB(*dbw,"cr+").ZapSelectTx(vHashIn, vHashOut);
4175-
for (uint256 hash : vHashOut)
4176-
mapWallet.erase(hash);
4177+
for (uint256 hash : vHashOut) {
4178+
const auto& it = mapWallet.find(hash);
4179+
wtxOrdered.erase(it->second.m_it_wtxOrdered);
4180+
mapWallet.erase(it);
4181+
}
41774182

41784183
if (nZapSelectTxRet == DB_NEED_REWRITE)
41794184
{

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ class CWalletTx : public CMerkleTx
331331
char fFromMe;
332332
std::string strFromAccount;
333333
int64_t nOrderPos; //!< position in ordered transaction list
334+
std::multimap<int64_t, std::pair<CWalletTx*, CAccountingEntry*>>::const_iterator m_it_wtxOrdered;
334335

335336
// memory only
336337
mutable bool fDebitCached;

0 commit comments

Comments
 (0)