Skip to content

Commit fa74718

Browse files
committed
Merge bitcoin/bitcoin#24026: Block unsafe std::string fs::path conversion copy_file calls
3a45dc3 Change type of `backup_file` parameter in RestoreWallet/restoreWallet (Hennadii Stepanov) 213172c refactor: Block unsafe std::string fs::path conversion copy_file calls (Hennadii Stepanov) Pull request description: This PR is an optional prerequisite for bitcoin/bitcoin#20744 "Use std::filesystem. Remove Boost Filesystem & System" which: - makes further code changes safer - prevents [some](https://cirrus-ci.com/task/6525835388649472) test failures on native Windows ACKs for top commit: ryanofsky: Code review ACK 3a45dc3. Looks great! Thanks for debugging and fixing this and making #20744 smaller! Tree-SHA512: c6dfaef6b45b9c268bc9ee9b943b9d679152c9d565ca4f86da8d33f8eb9b3cdbe9ba6df7b7578eacc0d00db6551048beff97419f86eb4b1d3182c43e2b4eb9a5
2 parents 80ceede + 3a45dc3 commit fa74718

File tree

6 files changed

+15
-6
lines changed

6 files changed

+15
-6
lines changed

src/fs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ static inline path operator+(path p1, path p2)
9292
return p1;
9393
}
9494

95+
// Disallow implicit std::string conversion for copy_file
96+
// to avoid locale-dependent encoding on Windows.
97+
static inline void copy_file(const path& from, const path& to, copy_option options)
98+
{
99+
boost::filesystem::copy_file(from, to, options);
100+
}
101+
95102
/**
96103
* Convert path object to byte string. On POSIX, paths natively are byte
97104
* strings, so this is trivial. On Windows, paths natively are Unicode, so an

src/interfaces/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_INTERFACES_WALLET_H
77

88
#include <consensus/amount.h>
9+
#include <fs.h>
910
#include <interfaces/chain.h> // For ChainClient
1011
#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
1112
#include <script/standard.h> // For CTxDestination
@@ -326,7 +327,7 @@ class WalletLoader : public ChainClient
326327
virtual std::string getWalletDir() = 0;
327328

328329
//! Restore backup wallet
329-
virtual std::unique_ptr<Wallet> restoreWallet(const std::string& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
330+
virtual std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
330331

331332
//! Return available wallets in wallet directory.
332333
virtual std::vector<std::string> listWalletDir() = 0;

src/wallet/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ class WalletLoaderImpl : public WalletLoader
557557
options.require_existing = true;
558558
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
559559
}
560-
std::unique_ptr<Wallet> restoreWallet(const std::string& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
560+
std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
561561
{
562562
DatabaseStatus status;
563563

src/wallet/rpc/backup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ RPCHelpMan restorewallet()
18871887
bilingual_str error;
18881888
std::vector<bilingual_str> warnings;
18891889

1890-
const std::shared_ptr<CWallet> wallet = RestoreWallet(context, fs::PathToString(backup_file), wallet_name, load_on_start, status, error, warnings);
1890+
const std::shared_ptr<CWallet> wallet = RestoreWallet(context, backup_file, wallet_name, load_on_start, status, error, warnings);
18911891

18921892
HandleWalletError(wallet, status, error);
18931893

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
357357
return wallet;
358358
}
359359

360-
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const std::string& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
360+
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
361361
{
362362
DatabaseOptions options;
363363
options.require_existing = true;
364364

365-
if (!fs::exists(fs::u8path(backup_file))) {
365+
if (!fs::exists(backup_file)) {
366366
error = Untranslated("Backup file does not exist");
367367
status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE;
368368
return nullptr;

src/wallet/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define BITCOIN_WALLET_WALLET_H
88

99
#include <consensus/amount.h>
10+
#include <fs.h>
1011
#include <interfaces/chain.h>
1112
#include <interfaces/handler.h>
1213
#include <outputtype.h>
@@ -60,7 +61,7 @@ std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context);
6061
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);
6162
std::shared_ptr<CWallet> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
6263
std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
63-
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const std::string& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
64+
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
6465
std::unique_ptr<interfaces::Handler> HandleLoadWallet(WalletContext& context, LoadWalletFn load_wallet);
6566
std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
6667

0 commit comments

Comments
 (0)