Skip to content

Commit 53ed42b

Browse files
laanwjchristiancfifi
authored andcommitted
bitcoin#14708: Warn unrecognised sections in the config file
3fb09b9 Warn unrecognized sections in the config file (Akio Nakamura) Pull request description: This PR intends to resolve bitcoin#14702. In the config file, sections are specified by square bracket pair "[]"$, or included in the option name itself which separated by a period"(.)". Typicaly, [testnet] is not a correct section name and specified options in that section are ignored but user cannot recognize what is happen. So, add some log-warning messages if unrecognized section names are present in the config file after checking section only args. note: Currentry, followings are out of scope of this PR. 1) Empty section name or option name can describe. e.g. [] , .a=b, =c 2) Multiple period characters can exist in the section name and option name. e.g. [c.d.e], [..], f.g.h.i=j, ..=k Tree-SHA512: 2cea02a0525feb40320613989a75cd7b7b1bd12158d5e6f3174ca77e6a25bb84425dd8812f62483df9fc482045c7b5402d69bc714430518b1847d055a2dc304b
1 parent b9d2e81 commit 53ed42b

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/init.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,15 @@ void InitParameterInteraction()
11131113
// Warn if network-specific options (-addnode, -connect, etc) are
11141114
// specified in default section of config file, but not overridden
11151115
// on the command line or in this network's section of the config file.
1116-
gArgs.WarnForSectionOnlyArgs();
1116+
std::string network = gArgs.GetChainName();
1117+
for (const auto& arg : gArgs.GetUnsuitableSectionOnlyArgs()) {
1118+
InitWarning(strprintf(_("Config setting for %s only applied on %s network when in [%s] section."), arg, network, network));
1119+
}
1120+
1121+
// Warn if unrecognized section name are present in the config file.
1122+
for (const auto& section : gArgs.GetUnrecognizedSections()) {
1123+
InitWarning(strprintf(_("Section [%s] is not recognized."), section));
1124+
}
11171125
}
11181126

11191127
static std::string ResolveErrMsg(const char * const optname, const std::string& strBind)

src/util/system.cpp

+35-7
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,17 @@ ArgsManager::ArgsManager() :
400400
// nothing to do
401401
}
402402

403-
void ArgsManager::WarnForSectionOnlyArgs()
403+
const std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
404404
{
405+
std::set<std::string> unsuitables;
406+
405407
LOCK(cs_args);
406408

407409
// if there's no section selected, don't worry
408-
if (m_network.empty()) return;
410+
if (m_network.empty()) return std::set<std::string> {};
409411

410412
// if it's okay to use the default section for this network, don't worry
411-
if (m_network == CBaseChainParams::MAIN) return;
413+
if (m_network == CBaseChainParams::MAIN) return std::set<std::string> {};
412414

413415
for (const auto& arg : m_network_only_args) {
414416
std::pair<bool, std::string> found_result;
@@ -426,8 +428,28 @@ void ArgsManager::WarnForSectionOnlyArgs()
426428
if (!found_result.first) continue;
427429

428430
// otherwise, issue a warning
429-
LogPrintf("Warning: Config setting for %s only applied on %s network when in [%s] section.\n", arg, m_network, m_network);
431+
unsuitables.insert(arg);
430432
}
433+
return unsuitables;
434+
}
435+
436+
437+
const std::set<std::string> ArgsManager::GetUnrecognizedSections() const
438+
{
439+
// Section names to be recognized in the config file.
440+
static const std::set<std::string> available_sections{
441+
CBaseChainParams::REGTEST,
442+
CBaseChainParams::TESTNET,
443+
CBaseChainParams::MAIN
444+
};
445+
std::set<std::string> diff;
446+
447+
LOCK(cs_args);
448+
std::set_difference(
449+
m_config_sections.begin(), m_config_sections.end(),
450+
available_sections.begin(), available_sections.end(),
451+
std::inserter(diff, diff.end()));
452+
return diff;
431453
}
432454

433455
void ArgsManager::SelectConfigNetwork(const std::string& network)
@@ -879,7 +901,7 @@ static std::string TrimString(const std::string& str, const std::string& pattern
879901
return str.substr(front, end - front + 1);
880902
}
881903

882-
static bool GetConfigOptions(std::istream& stream, std::string& error, std::vector<std::pair<std::string, std::string>> &options)
904+
static bool GetConfigOptions(std::istream& stream, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::set<std::string>& sections)
883905
{
884906
std::string str, prefix;
885907
std::string::size_type pos;
@@ -894,7 +916,9 @@ static bool GetConfigOptions(std::istream& stream, std::string& error, std::vect
894916
str = TrimString(str, pattern);
895917
if (!str.empty()) {
896918
if (*str.begin() == '[' && *str.rbegin() == ']') {
897-
prefix = str.substr(1, str.size() - 2) + '.';
919+
const std::string section = str.substr(1, str.size() - 2);
920+
sections.insert(section);
921+
prefix = section + '.';
898922
} else if (*str.begin() == '-') {
899923
error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
900924
return false;
@@ -906,6 +930,9 @@ static bool GetConfigOptions(std::istream& stream, std::string& error, std::vect
906930
return false;
907931
}
908932
options.emplace_back(name, value);
933+
if ((pos = name.rfind('.')) != std::string::npos) {
934+
sections.insert(name.substr(0, pos));
935+
}
909936
} else {
910937
error = strprintf("parse error on line %i: %s", linenr, str);
911938
if (str.size() >= 2 && str.substr(0, 2) == "no") {
@@ -923,7 +950,8 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, std::string& error, boo
923950
{
924951
LOCK(cs_args);
925952
std::vector<std::pair<std::string, std::string>> options;
926-
if (!GetConfigOptions(stream, error, options)) {
953+
m_config_sections.clear();
954+
if (!GetConfigOptions(stream, error, options, m_config_sections)) {
927955
return false;
928956
}
929957
for (const std::pair<std::string, std::string>& option : options) {

src/util/system.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class ArgsManager
182182
std::string m_network GUARDED_BY(cs_args);
183183
std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
184184
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
185+
std::set<std::string> m_config_sections GUARDED_BY(cs_args);
185186

186187
[[nodiscard]] bool ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys = false);
187188

@@ -202,7 +203,12 @@ class ArgsManager
202203
* on the command line or in a network-specific section in the
203204
* config file.
204205
*/
205-
void WarnForSectionOnlyArgs();
206+
const std::set<std::string> GetUnsuitableSectionOnlyArgs() const;
207+
208+
/**
209+
* Log warnings for unrecognized section names in the config file.
210+
*/
211+
const std::set<std::string> GetUnrecognizedSections() const;
206212

207213
/**
208214
* Return a vector of strings of the given argument

0 commit comments

Comments
 (0)