mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-31 08:13:52 +02:00
refactor: Block unsafe fs::path std::string conversion calls
There is no change in behavior. This just helps prepare for the transition from boost::filesystem to std::filesystem by avoiding calls to methods which will be unsafe after the transaction to std::filesystem to due lack of a boost::filesystem::path::imbue equivalent and inability to set a predictable locale. Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Co-authored-by: Kiminuo <kiminuo@protonmail.com> Co-authored-by: MarcoFalke <falke.marco@gmail.com>
This commit is contained in:
@@ -48,7 +48,7 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
|
||||
const bool start_with_corrupted_banlist{fuzzed_data_provider.ConsumeBool()};
|
||||
bool force_read_and_write_to_err{false};
|
||||
if (start_with_corrupted_banlist) {
|
||||
assert(WriteBinaryFile(banlist_file.string() + ".json",
|
||||
assert(WriteBinaryFile(banlist_file + ".json",
|
||||
fuzzed_data_provider.ConsumeRandomLengthString()));
|
||||
} else {
|
||||
force_read_and_write_to_err = fuzzed_data_provider.ConsumeBool();
|
||||
@@ -111,5 +111,5 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
|
||||
assert(banmap == banmap_read);
|
||||
}
|
||||
}
|
||||
fs::remove(banlist_file.string() + ".json");
|
||||
fs::remove(fs::PathToString(banlist_file + ".json"));
|
||||
}
|
||||
|
||||
@@ -80,19 +80,19 @@ BOOST_AUTO_TEST_CASE(ReadWrite)
|
||||
"dupe": "dupe"
|
||||
})");
|
||||
BOOST_CHECK(!util::ReadSettings(path, values, errors));
|
||||
std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", path.string())};
|
||||
std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
|
||||
|
||||
// Check non-kv json files not allowed
|
||||
WriteText(path, R"("non-kv")");
|
||||
BOOST_CHECK(!util::ReadSettings(path, values, errors));
|
||||
std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", path.string())};
|
||||
std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", fs::PathToString(path))};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end());
|
||||
|
||||
// Check invalid json not allowed
|
||||
WriteText(path, R"(invalid json)");
|
||||
BOOST_CHECK(!util::ReadSettings(path, values, errors));
|
||||
std::vector<std::string> fail_parse = {strprintf("Unable to parse settings file %s", path.string())};
|
||||
std::vector<std::string> fail_parse = {strprintf("Unable to parse settings file %s", fs::PathToString(path))};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end());
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleati
|
||||
|
||||
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile);
|
||||
BOOST_TEST_MESSAGE(
|
||||
"Wrote UTXO snapshot to " << snapshot_path.make_preferred().string() << ": " << result.write());
|
||||
"Wrote UTXO snapshot to " << fs::PathToString(snapshot_path.make_preferred()) << ": " << result.write());
|
||||
|
||||
// Read the written snapshot in and then activate it.
|
||||
//
|
||||
|
||||
@@ -91,8 +91,8 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
|
||||
extra_args);
|
||||
util::ThreadRename("test");
|
||||
fs::create_directories(m_path_root);
|
||||
m_args.ForceSetArg("-datadir", m_path_root.string());
|
||||
gArgs.ForceSetArg("-datadir", m_path_root.string());
|
||||
m_args.ForceSetArg("-datadir", fs::PathToString(m_path_root));
|
||||
gArgs.ForceSetArg("-datadir", fs::PathToString(m_path_root));
|
||||
gArgs.ClearPathCache();
|
||||
{
|
||||
SetupServerArgs(*m_node.args);
|
||||
|
||||
@@ -51,23 +51,23 @@ BOOST_AUTO_TEST_CASE(util_datadir)
|
||||
{
|
||||
// Use local args variable instead of m_args to avoid making assumptions about test setup
|
||||
ArgsManager args;
|
||||
args.ForceSetArg("-datadir", m_path_root.string());
|
||||
args.ForceSetArg("-datadir", fs::PathToString(m_path_root));
|
||||
|
||||
const fs::path dd_norm = args.GetDataDirBase();
|
||||
|
||||
args.ForceSetArg("-datadir", dd_norm.string() + "/");
|
||||
args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/");
|
||||
args.ClearPathCache();
|
||||
BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
|
||||
|
||||
args.ForceSetArg("-datadir", dd_norm.string() + "/.");
|
||||
args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/.");
|
||||
args.ClearPathCache();
|
||||
BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
|
||||
|
||||
args.ForceSetArg("-datadir", dd_norm.string() + "/./");
|
||||
args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/./");
|
||||
args.ClearPathCache();
|
||||
BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
|
||||
|
||||
args.ForceSetArg("-datadir", dd_norm.string() + "/.//");
|
||||
args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/.//");
|
||||
args.ClearPathCache();
|
||||
BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
|
||||
}
|
||||
@@ -1181,13 +1181,13 @@ BOOST_AUTO_TEST_CASE(util_ReadWriteSettings)
|
||||
{
|
||||
// Test writing setting.
|
||||
TestArgsManager args1;
|
||||
args1.ForceSetArg("-datadir", m_path_root.string());
|
||||
args1.ForceSetArg("-datadir", fs::PathToString(m_path_root));
|
||||
args1.LockSettings([&](util::Settings& settings) { settings.rw_settings["name"] = "value"; });
|
||||
args1.WriteSettingsFile();
|
||||
|
||||
// Test reading setting.
|
||||
TestArgsManager args2;
|
||||
args2.ForceSetArg("-datadir", m_path_root.string());
|
||||
args2.ForceSetArg("-datadir", fs::PathToString(m_path_root));
|
||||
args2.ReadSettingsFile();
|
||||
args2.LockSettings([&](util::Settings& settings) { BOOST_CHECK_EQUAL(settings.rw_settings["name"].get_str(), "value"); });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user