Skip to content

Commit a66c0f7

Browse files
committed
util: Report parse errors in configuration file
Report errors while parsing the configuration file, instead of silently ignoring them. $ src/bitcoind -regtest Error reading configuration file: parse error on line 22: nodebuglogfile, if you intended to specify a negated option, use nodebuglogfile=1 instead $ src/bitcoind -regtest Error reading configuration file: parse error on line 22: sdafsdfafs $ src/bitcoind -regtest Error reading configuration file: parse error on line 24: -nodebuglogfile=1, options in the configuration file must be specified without leading -
1 parent be301a5 commit a66c0f7

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/util.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,11 @@ static std::string TrimString(const std::string& str, const std::string& pattern
812812
return str.substr(front, end - front + 1);
813813
}
814814

815-
static std::vector<std::pair<std::string, std::string>> GetConfigOptions(std::istream& stream)
815+
static bool GetConfigOptions(std::istream& stream, std::string& error, std::vector<std::pair<std::string, std::string>> &options)
816816
{
817-
std::vector<std::pair<std::string, std::string>> options;
818817
std::string str, prefix;
819818
std::string::size_type pos;
819+
int linenr = 1;
820820
while (std::getline(stream, str)) {
821821
if ((pos = str.find('#')) != std::string::npos) {
822822
str = str.substr(0, pos);
@@ -826,21 +826,34 @@ static std::vector<std::pair<std::string, std::string>> GetConfigOptions(std::is
826826
if (!str.empty()) {
827827
if (*str.begin() == '[' && *str.rbegin() == ']') {
828828
prefix = str.substr(1, str.size() - 2) + '.';
829+
} else if (*str.begin() == '-') {
830+
error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
831+
return false;
829832
} else if ((pos = str.find('=')) != std::string::npos) {
830833
std::string name = prefix + TrimString(str.substr(0, pos), pattern);
831834
std::string value = TrimString(str.substr(pos + 1), pattern);
832835
options.emplace_back(name, value);
836+
} else {
837+
error = strprintf("parse error on line %i: %s", linenr, str);
838+
if (str.size() >= 2 && str.substr(0, 2) == "no") {
839+
error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str);
840+
}
841+
return false;
833842
}
834843
}
844+
++linenr;
835845
}
836-
return options;
846+
return true;
837847
}
838848

839849
bool ArgsManager::ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys)
840850
{
841851
LOCK(cs_args);
842-
843-
for (const std::pair<std::string, std::string>& option : GetConfigOptions(stream)) {
852+
std::vector<std::pair<std::string, std::string>> options;
853+
if (!GetConfigOptions(stream, error, options)) {
854+
return false;
855+
}
856+
for (const std::pair<std::string, std::string>& option : options) {
844857
std::string strKey = std::string("-") + option.first;
845858
std::string strValue = option.second;
846859

0 commit comments

Comments
 (0)