Skip to content

Commit

Permalink
wallet: Remove trailing separators from -walletdir arg
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRochard committed Sep 13, 2018
1 parent ea3009e commit 2d47163
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/wallet/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,18 @@ bool WalletInit::Verify() const

if (gArgs.IsArgSet("-walletdir")) {
fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
if (!fs::exists(wallet_dir)) {
boost::system::error_code error;
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
if (error || !fs::exists(wallet_dir)) {
return InitError(strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string()));
} else if (!fs::is_directory(wallet_dir)) {
return InitError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string()));
// The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
} else if (!wallet_dir.is_absolute()) {
return InitError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string()));
}
gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
}

LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
Expand Down
20 changes: 20 additions & 0 deletions src/wallet/test/init_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_is_not_relative)
BOOST_CHECK(result == false);
}

BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_no_trailing)
{
SetWalletDir(m_walletdir_path_cases["trailing"]);
bool result = g_wallet_init_interface.Verify();
BOOST_CHECK(result == true);
fs::path walletdir = gArgs.GetArg("-walletdir", "");
fs::path expected_path = fs::canonical(m_walletdir_path_cases["default"]);
BOOST_CHECK(walletdir == expected_path);
}

BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_no_trailing2)
{
SetWalletDir(m_walletdir_path_cases["trailing2"]);
bool result = g_wallet_init_interface.Verify();
BOOST_CHECK(result == true);
fs::path walletdir = gArgs.GetArg("-walletdir", "");
fs::path expected_path = fs::canonical(m_walletdir_path_cases["default"]);
BOOST_CHECK(walletdir == expected_path);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2d47163

Please sign in to comment.