Skip to content

Commit b4bc32a

Browse files
committed
[wallet] Get rid of CWalletTx default constructor
No change in behavior in the normal case. But buggy mapWallet lookups with invalid txids will now throw exceptions instead of inserting dummy entries into the map, and potentially causing segfaults and other failures. This also makes it a compiler error to use the mapWallet[hash] syntax which could create dummy entries.
1 parent a128bdc commit b4bc32a

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

src/wallet/test/accounting_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ GetResults(CWallet& wallet, std::map<CAmount, CAccountingEntry>& results)
2929
BOOST_AUTO_TEST_CASE(acc_orderupgrade)
3030
{
3131
std::vector<CWalletTx*> vpwtx;
32-
CWalletTx wtx;
32+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
3333
CAccountingEntry ae;
3434
std::map<CAmount, CAccountingEntry> results;
3535

@@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
4444

4545
wtx.mapValue["comment"] = "z";
4646
m_wallet.AddToWallet(wtx);
47-
vpwtx.push_back(&m_wallet.mapWallet[wtx.GetHash()]);
47+
vpwtx.push_back(&m_wallet.mapWallet.at(wtx.GetHash()));
4848
vpwtx[0]->nTimeReceived = (unsigned int)1333333335;
4949
vpwtx[0]->nOrderPos = -1;
5050

@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
8686
wtx.SetTx(MakeTransactionRef(std::move(tx)));
8787
}
8888
m_wallet.AddToWallet(wtx);
89-
vpwtx.push_back(&m_wallet.mapWallet[wtx.GetHash()]);
89+
vpwtx.push_back(&m_wallet.mapWallet.at(wtx.GetHash()));
9090
vpwtx[1]->nTimeReceived = (unsigned int)1333333336;
9191

9292
wtx.mapValue["comment"] = "x";
@@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
9696
wtx.SetTx(MakeTransactionRef(std::move(tx)));
9797
}
9898
m_wallet.AddToWallet(wtx);
99-
vpwtx.push_back(&m_wallet.mapWallet[wtx.GetHash()]);
99+
vpwtx.push_back(&m_wallet.mapWallet.at(wtx.GetHash()));
100100
vpwtx[2]->nTimeReceived = (unsigned int)1333333329;
101101
vpwtx[2]->nOrderPos = -1;
102102

src/wallet/wallet.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
531531
int nMinOrderPos = std::numeric_limits<int>::max();
532532
const CWalletTx* copyFrom = nullptr;
533533
for (TxSpends::iterator it = range.first; it != range.second; ++it) {
534-
const CWalletTx* wtx = &mapWallet[it->second];
534+
const CWalletTx* wtx = &mapWallet.at(it->second);
535535
if (wtx->nOrderPos < nMinOrderPos) {
536536
nMinOrderPos = wtx->nOrderPos;;
537537
copyFrom = wtx;
@@ -544,7 +544,7 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
544544
for (TxSpends::iterator it = range.first; it != range.second; ++it)
545545
{
546546
const uint256& hash = it->second;
547-
CWalletTx* copyTo = &mapWallet[hash];
547+
CWalletTx* copyTo = &mapWallet.at(hash);
548548
if (copyFrom == copyTo) continue;
549549
assert(copyFrom && "Oldest wallet transaction in range assumed to have been found.");
550550
if (!copyFrom->IsEquivalentTo(*copyTo)) continue;
@@ -3081,7 +3081,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
30813081
// Notify that old coins are spent
30823082
for (const CTxIn& txin : wtxNew.tx->vin)
30833083
{
3084-
CWalletTx &coin = mapWallet[txin.prevout.hash];
3084+
CWalletTx &coin = mapWallet.at(txin.prevout.hash);
30853085
coin.BindWallet(this);
30863086
NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
30873087
}
@@ -3092,7 +3092,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
30923092

30933093
// Get the inserted-CWalletTx from mapWallet so that the
30943094
// fInMempool flag is cached properly
3095-
CWalletTx& wtx = mapWallet[wtxNew.GetHash()];
3095+
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
30963096

30973097
if (fBroadcastTransactions)
30983098
{
@@ -3548,7 +3548,7 @@ std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
35483548
CTxDestination address;
35493549
if(!IsMine(txin)) /* If this input isn't mine, ignore it */
35503550
continue;
3551-
if(!ExtractDestination(mapWallet[txin.prevout.hash].tx->vout[txin.prevout.n].scriptPubKey, address))
3551+
if(!ExtractDestination(mapWallet.at(txin.prevout.hash).tx->vout[txin.prevout.n].scriptPubKey, address))
35523552
continue;
35533553
grouping.insert(address);
35543554
any_mine = true;

src/wallet/wallet.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,6 @@ class CWalletTx : public CMerkleTx
348348
mutable CAmount nAvailableWatchCreditCached;
349349
mutable CAmount nChangeCached;
350350

351-
CWalletTx()
352-
{
353-
Init(nullptr);
354-
}
355-
356351
CWalletTx(const CWallet* pwalletIn, CTransactionRef arg) : CMerkleTx(std::move(arg))
357352
{
358353
Init(pwalletIn);

src/wallet/walletdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
265265
{
266266
uint256 hash;
267267
ssKey >> hash;
268-
CWalletTx wtx;
268+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
269269
ssValue >> wtx;
270270
CValidationState state;
271271
if (!(CheckTransaction(*wtx.tx, state) && (wtx.GetHash() == hash) && state.IsValid()))
@@ -603,7 +603,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
603603
pwallet->UpdateTimeFirstKey(1);
604604

605605
for (uint256 hash : wss.vWalletUpgrade)
606-
WriteTx(pwallet->mapWallet[hash]);
606+
WriteTx(pwallet->mapWallet.at(hash));
607607

608608
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
609609
if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
@@ -664,7 +664,7 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
664664
uint256 hash;
665665
ssKey >> hash;
666666

667-
CWalletTx wtx;
667+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
668668
ssValue >> wtx;
669669

670670
vTxHash.push_back(hash);

0 commit comments

Comments
 (0)