From 6bb54708e6457f21596793a7149dc6dfea1dc871 Mon Sep 17 00:00:00 2001 From: nthumann Date: Wed, 4 Aug 2021 12:07:31 +0200 Subject: [PATCH 1/2] util: Check if specified config file cannot be opened --- src/util/system.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/util/system.cpp b/src/util/system.cpp index 258ba2f2355..85a23731a2e 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -904,6 +904,11 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) const std::string confPath = GetArg("-conf", BITCOIN_CONF_FILENAME); fsbridge::ifstream stream(GetConfigFile(confPath)); + // not ok to have a config file specified that cannot be opened + if (IsArgSet("-conf") && !stream.good()) { + error = strprintf("specified config file \"%s\" could not be opened.", confPath); + return false; + } // ok to not have a config file if (stream.good()) { if (!ReadConfigStream(stream, confPath, error, ignore_invalid_keys)) { From 127b4608e9dbb8217c74c9332e82fcec8c326fa8 Mon Sep 17 00:00:00 2001 From: nthumann Date: Wed, 4 Aug 2021 12:24:40 +0200 Subject: [PATCH 2/2] test: Check if specified config file cannot be opened --- test/functional/feature_config_args.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/functional/feature_config_args.py b/test/functional/feature_config_args.py index 24c8a8987ab..9e99cd549d5 100755 --- a/test/functional/feature_config_args.py +++ b/test/functional/feature_config_args.py @@ -248,6 +248,10 @@ class ConfArgsTest(BitcoinTestFramework): self.nodes[0].assert_start_raises_init_error(['-conf=' + conf_file], 'Error: Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.') + # Check that an explicitly specified config file that cannot be opened fails + none_existent_conf_file = os.path.join(default_data_dir, "none_existent_bitcoin.conf") + self.nodes[0].assert_start_raises_init_error(['-conf=' + none_existent_conf_file], 'Error: Error reading configuration file: specified config file "' + none_existent_conf_file + '" could not be opened.') + # Create the directory and ensure the config file now works os.mkdir(new_data_dir) self.start_node(0, ['-conf='+conf_file])