diff options
author | John Newbery <john@johnnewbery.com> | 2018-05-07 17:08:03 -0400 |
---|---|---|
committer | John Newbery <john@johnnewbery.com> | 2018-05-15 13:28:29 -0400 |
commit | 59b87a27efea819e433c727756bf5fac57b33dd6 (patch) | |
tree | 0e299fa24e8b6f2b8e4da9804be7f0352894b837 /src | |
parent | 13da2899ae42d48547e5cb491ea6aba4882b3306 (diff) |
[wallet] Fix potential memory leak in CreateWalletFromFile
Fix proposed by ryanofsky in
https://github.com/bitcoin/bitcoin/pull/12647#discussion_r174875670
Diffstat (limited to 'src')
-rw-r--r-- | src/wallet/wallet.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5be9f4a290..f9f567009a 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4012,7 +4012,10 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path& int64_t nStart = GetTimeMillis(); bool fFirstRun = true; - CWallet *walletInstance = new CWallet(name, WalletDatabase::Create(path)); + // Make a temporary wallet unique pointer so memory doesn't get leaked if + // wallet creation fails. + auto temp_wallet = MakeUnique<CWallet>(name, WalletDatabase::Create(path)); + CWallet* walletInstance = temp_wallet.get(); DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun); if (nLoadWalletRet != DBErrors::LOAD_OK) { @@ -4224,7 +4227,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path& } walletInstance->m_last_block_processed = chainActive.Tip(); - RegisterValidationInterface(walletInstance); if (chainActive.Tip() && chainActive.Tip() != pindexRescan) { @@ -4290,6 +4292,10 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path& } } } + + // Register with the validation interface. It's ok to do this after rescan since we're still holding cs_main. + RegisterValidationInterface(temp_wallet.release()); + walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST)); { |