From e85abe92c7cc5380489c028479f0d42f91827efd Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:31:19 +0100 Subject: [PATCH] args: Catch directories in place of config files Previously passing a directory path as -conf would lead to an ifstream being opened for it, and would not trigger any errors. --- src/common/config.cpp | 12 +++++++++++- src/init/common.cpp | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 13db98171ae..fac4aa314c5 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -130,6 +131,10 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) const auto conf_path{GetConfigFilePath()}; std::ifstream stream; if (!conf_path.empty()) { // path is empty when -noconf is specified + if (fs::is_directory(conf_path)) { + error = strprintf("Config file \"%s\" is a directory.", fs::PathToString(conf_path)); + return false; + } stream = std::ifstream{conf_path}; // If the file is explicitly specified, it must be readable if (IsArgSet("-conf") && !stream.good()) { @@ -177,7 +182,12 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) const size_t default_includes = add_includes({}); 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)}; + const auto include_conf_path{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)}; + if (fs::is_directory(include_conf_path)) { + error = strprintf("Included config file \"%s\" is a directory.", fs::PathToString(include_conf_path)); + return false; + } + std::ifstream conf_file_stream{include_conf_path}; if (conf_file_stream.good()) { if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) { return false; diff --git a/src/init/common.cpp b/src/init/common.cpp index 8f7a86b6886..3a8d75a6269 100644 --- a/src/init/common.cpp +++ b/src/init/common.cpp @@ -125,6 +125,8 @@ bool StartLogging(const ArgsManager& args) fs::path config_file_path = args.GetConfigFilePath(); if (args.IsArgNegated("-conf")) { LogInfo("Config file: "); + } else if (fs::is_directory(config_file_path)) { + LogWarning("Config file: %s (is directory, not file)", fs::PathToString(config_file_path)); } else if (fs::exists(config_file_path)) { LogPrintf("Config file: %s\n", fs::PathToString(config_file_path)); } else if (args.IsArgSet("-conf")) {