From 20e950b2b875751d5b5cf70e4c0fe6d93d654eda Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Thu, 14 Nov 2024 23:17:19 +0100 Subject: [PATCH] args: Properly support -noconf and catch directories peek() in order to catch directories being specified instead of configuration files. Previously passing a directory path as -conf would lead to an ifstream being opened for it, and would not trigger any errors. -noconf would previously lead to an ifstream "successfully" being opened to the ".bitcoin"-directory (not a file)). With the change to AbsPathForConfigVal we will now try to open the marginally more correct absolute path "". Other users of AbsPathForConfigVal() have been updated to handle negation in 2 prior commits. --- src/common/config.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 98223fc3e3a59b..518c11338eb6f9 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -129,9 +129,11 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) const auto conf_path{GetConfigFilePath()}; std::ifstream stream{conf_path}; + // Calling peek() on ifstreams opened on directories flags them as !good(). + (void)stream.peek(); // not ok to have a config file specified that cannot be opened - if (IsArgSet("-conf") && !stream.good()) { + if ((IsArgSet("-conf") && !IsArgNegated("-conf")) && !stream.good()) { error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path)); return false; } @@ -176,6 +178,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) for (const std::string& conf_file_name : conf_file_names) { std::ifstream conf_file_stream{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)}; + (void)conf_file_stream.peek(); if (conf_file_stream.good()) { if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) { return false; @@ -213,7 +216,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific) { - if (path.is_absolute()) { + if (path.is_absolute() || path.empty()) { return path; } return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);