fix possible memory leaks in CWallet::EncryptWallet

- add missing deletes for pwalletdbEncryption
- add an assert before trying to reserve memory for pwalletdbEncryption
- add a destructor to CWallet, which ensures deletion of
  pwalletdbEncryption on object destruction
This commit is contained in:
Philip Kaufmann 2014-09-28 16:11:17 +02:00
parent f606bb9baf
commit 870da77da6
2 changed files with 26 additions and 6 deletions

View File

@ -426,17 +426,25 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey; mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
if (fFileBacked) if (fFileBacked)
{ {
assert(!pwalletdbEncryption);
pwalletdbEncryption = new CWalletDB(strWalletFile); pwalletdbEncryption = new CWalletDB(strWalletFile);
if (!pwalletdbEncryption->TxnBegin()) if (!pwalletdbEncryption->TxnBegin()) {
delete pwalletdbEncryption;
pwalletdbEncryption = NULL;
return false; return false;
}
pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey); pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
} }
if (!EncryptKeys(vMasterKey)) if (!EncryptKeys(vMasterKey))
{ {
if (fFileBacked) if (fFileBacked) {
pwalletdbEncryption->TxnAbort(); pwalletdbEncryption->TxnAbort();
exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet. delete pwalletdbEncryption;
}
// We now probably have half of our keys encrypted in memory, and half not...
// die and let the user reload their unencrypted wallet.
exit(1);
} }
// Encryption was introduced in version 0.4.0 // Encryption was introduced in version 0.4.0
@ -444,8 +452,12 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
if (fFileBacked) if (fFileBacked)
{ {
if (!pwalletdbEncryption->TxnCommit()) if (!pwalletdbEncryption->TxnCommit()) {
exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet. delete pwalletdbEncryption;
// We now have keys encrypted in memory, but no on disk...
// die to avoid confusion and let the user reload their unencrypted wallet.
exit(1);
}
delete pwalletdbEncryption; delete pwalletdbEncryption;
pwalletdbEncryption = NULL; pwalletdbEncryption = NULL;
@ -1068,7 +1080,7 @@ int64_t CWallet::GetWatchOnlyBalance() const
nTotal += pcoin->GetAvailableWatchOnlyCredit(); nTotal += pcoin->GetAvailableWatchOnlyCredit();
} }
} }
return nTotal; return nTotal;
} }

View File

@ -143,6 +143,7 @@ public:
{ {
SetNull(); SetNull();
} }
CWallet(std::string strWalletFileIn) CWallet(std::string strWalletFileIn)
{ {
SetNull(); SetNull();
@ -150,6 +151,13 @@ public:
strWalletFile = strWalletFileIn; strWalletFile = strWalletFileIn;
fFileBacked = true; fFileBacked = true;
} }
~CWallet()
{
delete pwalletdbEncryption;
pwalletdbEncryption = NULL;
}
void SetNull() void SetNull()
{ {
nWalletVersion = FEATURE_BASE; nWalletVersion = FEATURE_BASE;