Coverage Report

Created: 2026-09-15 16:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/common/config.cpp
Line
Count
Source
1
// Copyright (c) 2023-present 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 <common/args.h> // IWYU pragma: associated
6
7
#include <common/settings.h>
8
#include <sync.h>
9
#include <tinyformat.h>
10
#include <univalue.h>
11
#include <util/check.h>
12
#include <util/fs.h>
13
#include <util/log.h>
14
#include <util/string.h>
15
16
#include <algorithm>
17
#include <cstddef>
18
#include <fstream>
19
#include <iostream>
20
#include <list>
21
#include <map>
22
#include <optional>
23
#include <sstream>
24
#include <string>
25
#include <string_view>
26
#include <utility>
27
#include <vector>
28
29
using util::TrimString;
30
using util::TrimStringView;
31
32
static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
33
0
{
34
0
    std::string str, prefix;
35
0
    std::string::size_type pos;
36
0
    int linenr = 1;
37
0
    while (std::getline(stream, str)) {
  Branch (37:12): [True: 0, False: 0]
38
0
        bool used_hash = false;
39
0
        if ((pos = str.find('#')) != std::string::npos) {
  Branch (39:13): [True: 0, False: 0]
40
0
            str = str.substr(0, pos);
41
0
            used_hash = true;
42
0
        }
43
0
        const static std::string pattern = " \t\r\n";
44
0
        str = TrimString(str, pattern);
45
0
        if (!str.empty()) {
  Branch (45:13): [True: 0, False: 0]
46
0
            if (*str.begin() == '[' && *str.rbegin() == ']') {
  Branch (46:17): [True: 0, False: 0]
  Branch (46:17): [True: 0, False: 0]
  Branch (46:40): [True: 0, False: 0]
47
0
                const std::string section = str.substr(1, str.size() - 2);
48
0
                sections.emplace_back(SectionInfo{section, filepath, linenr});
49
0
                prefix = section + '.';
50
0
            } else if (*str.begin() == '-') {
  Branch (50:24): [True: 0, False: 0]
51
0
                error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
52
0
                return false;
53
0
            } else if ((pos = str.find('=')) != std::string::npos) {
  Branch (53:24): [True: 0, False: 0]
54
0
                std::string name = prefix + TrimString(std::string_view{str}.substr(0, pos), pattern);
55
0
                std::string_view value = TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
56
0
                if (used_hash && name.find("rpcpassword") != std::string::npos) {
  Branch (56:21): [True: 0, False: 0]
  Branch (56:34): [True: 0, False: 0]
57
0
                    error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
58
0
                    return false;
59
0
                }
60
0
                options.emplace_back(name, value);
61
0
                if ((pos = name.rfind('.')) != std::string::npos && prefix.length() <= pos) {
  Branch (61:21): [True: 0, False: 0]
  Branch (61:69): [True: 0, False: 0]
62
0
                    sections.emplace_back(SectionInfo{name.substr(0, pos), filepath, linenr});
63
0
                }
64
0
            } else {
65
0
                error = strprintf("parse error on line %i: %s", linenr, str);
66
0
                if (str.size() >= 2 && str.starts_with("no")) {
  Branch (66:21): [True: 0, False: 0]
  Branch (66:40): [True: 0, False: 0]
67
0
                    error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str);
68
0
                }
69
0
                return false;
70
0
            }
71
0
        }
72
0
        ++linenr;
73
0
    }
74
0
    return true;
75
0
}
76
77
0
bool IsConfSupported(KeyInfo& key, std::string& error) {
78
0
    if (key.name == "conf") {
  Branch (78:9): [True: 0, False: 0]
79
0
        error = "conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";
80
0
        return false;
81
0
    }
82
0
    if (key.name == "reindex") {
  Branch (82:9): [True: 0, False: 0]
83
        // reindex can be set in a config file but it is strongly discouraged as this will cause the node to reindex on
84
        // every restart. Allow the config but throw a warning
85
0
        LogWarning("reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary");
86
0
        return true;
87
0
    }
88
0
    return true;
89
0
}
90
91
bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys)
92
0
{
93
0
    LOCK(cs_args);
94
0
    std::vector<std::pair<std::string, std::string>> options;
95
0
    if (!GetConfigOptions(stream, filepath, error, options, m_config_sections)) {
  Branch (95:9): [True: 0, False: 0]
96
0
        return false;
97
0
    }
98
0
    for (const std::pair<std::string, std::string>& option : options) {
  Branch (98:60): [True: 0, False: 0]
99
0
        KeyInfo key = InterpretKey(option.first);
100
0
        std::optional<unsigned int> flags = GetArgFlags_('-' + key.name);
101
0
        if (!IsConfSupported(key, error)) return false;
  Branch (101:13): [True: 0, False: 0]
102
0
        if (flags) {
  Branch (102:13): [True: 0, False: 0]
103
0
            std::optional<common::SettingsValue> value = InterpretValue(key, &option.second, *flags, error);
104
0
            if (!value) {
  Branch (104:17): [True: 0, False: 0]
105
0
                return false;
106
0
            }
107
0
            m_settings.ro_config[key.section][key.name].push_back(*value);
108
0
        } else {
109
0
            if (ignore_invalid_keys) {
  Branch (109:17): [True: 0, False: 0]
110
0
                LogWarning("Ignoring unknown configuration value %s", option.first);
111
0
            } else {
112
0
                error = strprintf("Invalid configuration value %s", option.first);
113
0
                return false;
114
0
            }
115
0
        }
116
0
    }
117
0
    return true;
118
0
}
119
120
bool ArgsManager::ReadConfigString(const std::string& str_config)
121
0
{
122
0
    std::istringstream streamConfig(str_config);
123
0
    {
124
0
        LOCK(cs_args);
125
0
        m_settings.ro_config.clear();
126
0
        m_config_sections.clear();
127
0
    }
128
0
    std::string error;
129
0
    return ReadConfigStream(streamConfig, "", error);
130
0
}
131
132
bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
133
0
{
134
0
    {
135
0
        LOCK(cs_args);
136
0
        m_settings.ro_config.clear();
137
0
        m_config_sections.clear();
138
0
        const auto conf_val = GetPathArg_("-conf", BITCOIN_CONF_FILENAME);
139
0
        m_config_path = (conf_val.is_absolute() || conf_val.empty()) ? conf_val : fsbridge::AbsPathJoin(GetDataDir(/*net_specific=*/false), conf_val);
  Branch (139:26): [True: 0, False: 0]
  Branch (139:52): [True: 0, False: 0]
140
0
    }
141
142
0
    const auto conf_path{GetConfigFilePath()};
143
0
    std::ifstream stream;
144
0
    if (!conf_path.empty()) { // path is empty when -noconf is specified
  Branch (144:9): [True: 0, False: 0]
145
0
        if (fs::is_directory(conf_path)) {
  Branch (145:13): [True: 0, False: 0]
146
0
            error = strprintf("Config file \"%s\" is a directory.", fs::PathToString(conf_path));
147
0
            return false;
148
0
        }
149
0
        stream = std::ifstream{conf_path.std_path()};
150
        // If the file is explicitly specified, it must be readable
151
0
        if (IsArgSet("-conf") && !stream.good()) {
  Branch (151:13): [True: 0, False: 0]
  Branch (151:13): [True: 0, False: 0]
  Branch (151:34): [True: 0, False: 0]
152
0
            error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
153
0
            return false;
154
0
        }
155
0
    }
156
    // ok to not have a config file
157
0
    if (stream.good()) {
  Branch (157:9): [True: 0, False: 0]
158
0
        if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) {
  Branch (158:13): [True: 0, False: 0]
159
0
            return false;
160
0
        }
161
        // `-includeconf` cannot be included in the command line arguments except
162
        // as `-noincludeconf` (which indicates that no included conf file should be used).
163
0
        bool use_conf_file{true};
164
0
        {
165
0
            LOCK(cs_args);
166
0
            if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
  Branch (166:23): [True: 0, False: 0]
167
                // ParseParameters() fails if a non-negated -includeconf is passed on the command-line
168
0
                assert(common::SettingsSpan(*includes).last_negated());
  Branch (168:17): [True: 0, False: 0]
169
0
                use_conf_file = false;
170
0
            }
171
0
        }
172
0
        if (use_conf_file) {
  Branch (172:13): [True: 0, False: 0]
173
0
            std::string chain_id = GetChainTypeString();
174
0
            std::vector<std::string> conf_file_names;
175
176
0
            auto add_includes = [&](const std::string& network, size_t skip = 0) {
177
0
                size_t num_values = 0;
178
0
                LOCK(cs_args);
179
0
                if (auto* section = common::FindKey(m_settings.ro_config, network)) {
  Branch (179:27): [True: 0, False: 0]
180
0
                    if (auto* values = common::FindKey(*section, "includeconf")) {
  Branch (180:31): [True: 0, False: 0]
181
0
                        for (size_t i = std::max(skip, common::SettingsSpan(*values).negated()); i < values->size(); ++i) {
  Branch (181:98): [True: 0, False: 0]
182
0
                            conf_file_names.push_back((*values)[i].get_str());
183
0
                        }
184
0
                        num_values = values->size();
185
0
                    }
186
0
                }
187
0
                return num_values;
188
0
            };
189
190
            // We haven't set m_network yet (that happens in SelectParams()), so manually check
191
            // for network.includeconf args.
192
0
            const size_t chain_includes = add_includes(chain_id);
193
0
            const size_t default_includes = add_includes({});
194
195
0
            for (const std::string& conf_file_name : conf_file_names) {
  Branch (195:52): [True: 0, False: 0]
196
0
                const auto include_conf_path{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)};
197
0
                if (fs::is_directory(include_conf_path)) {
  Branch (197:21): [True: 0, False: 0]
198
0
                    error = strprintf("Included config file \"%s\" is a directory.", fs::PathToString(include_conf_path));
199
0
                    return false;
200
0
                }
201
0
                std::ifstream conf_file_stream{include_conf_path.std_path()};
202
0
                if (conf_file_stream.good()) {
  Branch (202:21): [True: 0, False: 0]
203
0
                    if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
  Branch (203:25): [True: 0, False: 0]
204
0
                        return false;
205
0
                    }
206
0
                    LogInfo("Included configuration file %s\n", conf_file_name);
207
0
                } else {
208
0
                    error = "Failed to include configuration file " + conf_file_name;
209
0
                    return false;
210
0
                }
211
0
            }
212
213
            // Warn about recursive -includeconf
214
0
            conf_file_names.clear();
215
0
            add_includes(chain_id, /* skip= */ chain_includes);
216
0
            add_includes({}, /* skip= */ default_includes);
217
0
            std::string chain_id_final = GetChainTypeString();
218
0
            if (chain_id_final != chain_id) {
  Branch (218:17): [True: 0, False: 0]
219
                // Also warn about recursive includeconf for the chain that was specified in one of the includeconfs
220
0
                add_includes(chain_id_final);
221
0
            }
222
0
            for (const std::string& conf_file_name : conf_file_names) {
  Branch (222:52): [True: 0, False: 0]
223
0
                tfm::format(std::cerr, "warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
224
0
            }
225
0
        }
226
0
    }
227
228
    // If datadir is changed in .conf file:
229
0
    ClearPathCache();
230
0
    if (!CheckDataDirOption(*this)) {
  Branch (230:9): [True: 0, False: 0]
231
0
        error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", ""));
232
0
        return false;
233
0
    }
234
0
    return true;
235
0
}
236
237
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
238
0
{
239
0
    if (path.is_absolute() || path.empty()) {
  Branch (239:9): [True: 0, False: 0]
  Branch (239:31): [True: 0, False: 0]
240
0
        return path;
241
0
    }
242
0
    return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);
  Branch (242:34): [True: 0, False: 0]
243
0
}