0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-10 10:52:31 -05:00

Bugfix: Wallet: Wrap RestoreWallet content in a try block to ensure exceptions become returned errors and incomplete wallet directory is removed

This commit is contained in:
Luke Dashjr 2022-09-04 21:28:55 +00:00
parent 07df6cda14
commit 335ff98c8a

View file

@ -379,25 +379,31 @@ std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& b
ReadDatabaseArgs(*context.args, options); ReadDatabaseArgs(*context.args, options);
options.require_existing = true; options.require_existing = true;
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
auto wallet_file = wallet_path / "wallet.dat";
std::shared_ptr<CWallet> wallet;
try {
if (!fs::exists(backup_file)) { if (!fs::exists(backup_file)) {
error = Untranslated("Backup file does not exist"); error = Untranslated("Backup file does not exist");
status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE; status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE;
return nullptr; return nullptr;
} }
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) { if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path))); error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)));
status = DatabaseStatus::FAILED_ALREADY_EXISTS; status = DatabaseStatus::FAILED_ALREADY_EXISTS;
return nullptr; return nullptr;
} }
auto wallet_file = wallet_path / "wallet.dat";
fs::copy_file(backup_file, wallet_file, fs::copy_options::none); fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings); wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
} catch (const std::exception& e) {
assert(!wallet);
if (!error.empty()) error += Untranslated("\n");
error += strprintf(Untranslated("Unexpected exception: %s"), e.what());
}
if (!wallet) { if (!wallet) {
fs::remove(wallet_file); fs::remove(wallet_file);
fs::remove(wallet_path); fs::remove(wallet_path);