Skip to content

Commit 335ff98

Browse files
committed
Bugfix: Wallet: Wrap RestoreWallet content in a try block to ensure exceptions become returned errors and incomplete wallet directory is removed
1 parent 07df6cd commit 335ff98

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/wallet/wallet.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,25 +379,31 @@ std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& b
379379
ReadDatabaseArgs(*context.args, options);
380380
options.require_existing = true;
381381

382-
if (!fs::exists(backup_file)) {
383-
error = Untranslated("Backup file does not exist");
384-
status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE;
385-
return nullptr;
386-
}
387-
388382
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
383+
auto wallet_file = wallet_path / "wallet.dat";
384+
std::shared_ptr<CWallet> wallet;
389385

390-
if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
391-
error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)));
392-
status = DatabaseStatus::FAILED_ALREADY_EXISTS;
393-
return nullptr;
394-
}
386+
try {
387+
if (!fs::exists(backup_file)) {
388+
error = Untranslated("Backup file does not exist");
389+
status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE;
390+
return nullptr;
391+
}
395392

396-
auto wallet_file = wallet_path / "wallet.dat";
397-
fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
393+
if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
394+
error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)));
395+
status = DatabaseStatus::FAILED_ALREADY_EXISTS;
396+
return nullptr;
397+
}
398398

399-
auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
399+
fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
400400

401+
wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
402+
} catch (const std::exception& e) {
403+
assert(!wallet);
404+
if (!error.empty()) error += Untranslated("\n");
405+
error += strprintf(Untranslated("Unexpected exception: %s"), e.what());
406+
}
401407
if (!wallet) {
402408
fs::remove(wallet_file);
403409
fs::remove(wallet_path);

0 commit comments

Comments
 (0)