Skip to content

Commit

Permalink
refactor: Fix unreachable code in init arg checks
Browse files Browse the repository at this point in the history
Building with -Wunreachable-code-loop-increment causes a warning
due to always returning on the first iteration of the loop that
outputs errors on invalid args.

Collect all errors, and output them in a single error message
after the loop completes, resolving the warning and avoiding
popup hell by outputting a seperate message for each error.
  • Loading branch information
jonathan-schoeller-rea committed Jun 1, 2020
1 parent 091cc4b commit d15db4b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/init.cpp
Expand Up @@ -964,17 +964,27 @@ bool AppInitParameterInteraction()

// also see: InitParameterInteraction()

// Warn if network-specific options (-addnode, -connect, etc) are
// Error if network-specific options (-addnode, -connect, etc) are
// specified in default section of config file, but not overridden
// on the command line or in this network's section of the config file.
std::string network = gArgs.GetChainName();
bilingual_str errors;
for (const auto& arg : gArgs.GetUnsuitableSectionOnlyArgs()) {
return InitError(strprintf(_("Config setting for %s only applied on %s network when in [%s] section."), arg, network, network));
errors += strprintf(_("Config setting for %s only applied on %s network when in [%s] section.") + Untranslated("\n"), arg, network, network);
}

if (!errors.empty()) {
return InitError(errors);
}

// Warn if unrecognized section name are present in the config file.
bilingual_str warnings;
for (const auto& section : gArgs.GetUnrecognizedSections()) {
InitWarning(strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized."), section.m_file, section.m_line, section.m_name));
warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.") + Untranslated("\n"), section.m_file, section.m_line, section.m_name);
}

if (!warnings.empty()) {
InitWarning(warnings);
}

if (!fs::is_directory(GetBlocksDir())) {
Expand Down
5 changes: 5 additions & 0 deletions src/util/translation.h
Expand Up @@ -23,6 +23,11 @@ struct bilingual_str {
translated += rhs.translated;
return *this;
}

bool empty() const
{
return original.empty();
}
};

inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_config_args.py
Expand Up @@ -71,7 +71,7 @@ def test_config_file_parser(self):
with open(inc_conf_file2_path, 'w', encoding='utf-8') as conf:
conf.write('[testnet]\n')
self.restart_node(0)
self.nodes[0].stop_node(expected_stderr='Warning: ' + inc_conf_file_path + ':1 Section [testnot] is not recognized.' + os.linesep + 'Warning: ' + inc_conf_file2_path + ':1 Section [testnet] is not recognized.')
self.nodes[0].stop_node(expected_stderr='Warning: ' + inc_conf_file_path + ':1 Section [testnot] is not recognized.' + os.linesep + inc_conf_file2_path + ':1 Section [testnet] is not recognized.')

with open(inc_conf_file_path, 'w', encoding='utf-8') as conf:
conf.write('') # clear
Expand Down

0 comments on commit d15db4b

Please sign in to comment.