Skip to content

Commit 5a28a99

Browse files
committed
Refactor to GetWalletDir
1 parent 2c25e69 commit 5a28a99

File tree

5 files changed

+36
-50
lines changed

5 files changed

+36
-50
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ BITCOIN_TESTS += \
101101
wallet/test/wallet_tests.cpp \
102102
wallet/test/wallet_crypto_tests.cpp \
103103
wallet/test/coinselector_tests.cpp \
104-
wallet/test/db_tests.cpp
104+
wallet/test/walletutil_tests.cpp
105105

106106
BITCOIN_TEST_SUITE += \
107107
wallet/test/wallet_test_fixture.cpp \

src/wallet/db.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,8 @@ BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& data
7171
env_directory = wallet_path;
7272
database_filename = "wallet.dat";
7373
}
74-
// Ensure that the directory does not end with a trailing separator to avoid
75-
// creating two Berkeley environments in the same directory
76-
while ((env_directory.string().back() == '/') || (env_directory.string().back() == '\\'))
77-
env_directory = env_directory.remove_trailing_separator();
78-
7974
LOCK(cs_db);
80-
// Note: An unused temporary BerkeleyEnvironment object may be created inside the
75+
// Note: An ununsed temporary BerkeleyEnvironment object may be created inside the
8176
// emplace function if the key already exists. This is a little inefficient,
8277
// but not a big concern since the map will be changed in the future to hold
8378
// pointers instead of objects, anyway.

src/wallet/test/db_tests.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <boost/test/unit_test.hpp>
6+
7+
#include <test/test_bitcoin.h>
8+
#include <wallet/walletutil.h>
9+
10+
11+
BOOST_FIXTURE_TEST_SUITE(walletutil_tests, TestingSetup)
12+
13+
/** If a user passes in a path with a trailing separator as the walletdir, multiple BerkeleyEnvironments
14+
* may be created in the same directory which can lead to data corruption.
15+
*/
16+
BOOST_AUTO_TEST_CASE(get_wallet_dir_trailing_separators)
17+
{
18+
fs::path expected_wallet_dir = SetDataDir("get_wallet_dir");
19+
std::string trailing_separators;
20+
for (int i = 0; i < 4; ++i) {
21+
trailing_separators += fs::path::preferred_separator;
22+
gArgs.ForceSetArg("-walletdir", (expected_wallet_dir / trailing_separators).string());
23+
fs::path wallet_dir = GetWalletDir();
24+
BOOST_CHECK(wallet_dir == expected_wallet_dir);
25+
}
26+
}
27+
28+
BOOST_AUTO_TEST_SUITE_END()

src/wallet/walletutil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ fs::path GetWalletDir()
2323
}
2424
}
2525

26+
// Ensure that the directory does not end with a trailing separator to avoid
27+
// creating two Berkeley environments in the same directory
28+
while (fs::detail::is_directory_separator(path.string().back())) {
29+
path.remove_trailing_separator();
30+
}
31+
2632
return path;
2733
}

0 commit comments

Comments
 (0)