Browse Source

Merge pull request #4996

d0c4197 change exit(1) to an assert in CWallet::EncryptWallet (Philip Kaufmann)
870da77 fix possible memory leaks in CWallet::EncryptWallet (Philip Kaufmann)
f606bb9 fix a possible memory leak in CWalletDB::Recover (Philip Kaufmann)
0.10
Pieter Wuille 10 years ago
parent
commit
dec58922d0
No known key found for this signature in database
GPG Key ID: 57896D2FF8F0B657
  1. 26
      src/wallet.cpp
  2. 8
      src/wallet.h
  3. 7
      src/walletdb.cpp

26
src/wallet.cpp

@ -15,6 +15,8 @@
#include "util.h" #include "util.h"
#include "utilmoneystr.h" #include "utilmoneystr.h"
#include <assert.h>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
@ -426,17 +428,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.
assert(false);
} }
// Encryption was introduced in version 0.4.0 // Encryption was introduced in version 0.4.0
@ -444,8 +454,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.
assert(false);
}
delete pwalletdbEncryption; delete pwalletdbEncryption;
pwalletdbEncryption = NULL; pwalletdbEncryption = NULL;
@ -1068,7 +1082,7 @@ CAmount CWallet::GetWatchOnlyBalance() const
nTotal += pcoin->GetAvailableWatchOnlyCredit(); nTotal += pcoin->GetAvailableWatchOnlyCredit();
} }
} }
return nTotal; return nTotal;
} }

8
src/wallet.h

@ -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;

7
src/walletdb.cpp

@ -15,11 +15,11 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
using namespace std;
using namespace boost; using namespace boost;
using namespace std;
static uint64_t nAccountingEntryNumber = 0; static uint64_t nAccountingEntryNumber = 0;
@ -918,7 +918,7 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
bool fSuccess = allOK; bool fSuccess = allOK;
Db* pdbCopy = new Db(&dbenv.dbenv, 0); boost::scoped_ptr<Db> pdbCopy(new Db(&dbenv.dbenv, 0));
int ret = pdbCopy->open(NULL, // Txn pointer int ret = pdbCopy->open(NULL, // Txn pointer
filename.c_str(), // Filename filename.c_str(), // Filename
"main", // Logical db name "main", // Logical db name
@ -959,7 +959,6 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
} }
ptxn->commit(0); ptxn->commit(0);
pdbCopy->close(0); pdbCopy->close(0);
delete pdbCopy;
return fSuccess; return fSuccess;
} }

Loading…
Cancel
Save