Skip to content

Commit

Permalink
merge bitcoin#16097: Check IsArgKnown() early
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Nov 11, 2021
1 parent 05c133e commit b67fead
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,20 +439,20 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
if (key.length() > 1 && key[1] == '-')
key.erase(0, 1);

// Check for -nofoo
if (InterpretNegatedOption(key, val)) {
m_override_args[key].clear();
} else {
m_override_args[key].push_back(val);
}

// Check that the arg is known
if (!(IsSwitchChar(key[0]) && key.size() == 1)) {
if (!IsArgKnown(key)) {
error = strprintf("Invalid parameter %s", key.c_str());
return false;
}
}

// Check for -nofoo
if (InterpretNegatedOption(key, val)) {
m_override_args[key].clear();
} else {
m_override_args[key].push_back(val);
}
}

// we do not allow -includeconf from command line, so we clear it here
Expand All @@ -470,17 +470,23 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin

bool ArgsManager::IsArgKnown(const std::string& key) const
{
assert(key[0] == '-');

size_t option_index = key.find('.');
std::string arg_no_net;
if (option_index == std::string::npos) {
arg_no_net = key;
option_index = 1;
} else {
arg_no_net = std::string("-") + key.substr(option_index + 1, std::string::npos);
++option_index;
}
if (key.substr(option_index, 2) == "no") {
option_index += 2;
}

const std::string base_arg_name = '-' + key.substr(option_index);

LOCK(cs_args);
for (const auto& arg_map : m_available_args) {
if (arg_map.second.count(arg_no_net)) return true;
if (arg_map.second.count(base_arg_name)) return true;
}
return false;
}
Expand Down Expand Up @@ -910,23 +916,23 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
}
for (const std::pair<std::string, std::string>& option : options) {
std::string strKey = std::string("-") + option.first;
std::string strValue = option.second;

if (InterpretNegatedOption(strKey, strValue)) {
m_config_args[strKey].clear();
} else {
m_config_args[strKey].push_back(strValue);
}

// Check that the arg is known
if (!IsArgKnown(strKey)) {
if (!ignore_invalid_keys) {
error = strprintf("Invalid configuration value %s", option.first.c_str());
return false;
} else {
LogPrintf("Ignoring unknown configuration value %s\n", option.first);
continue;
}
}

std::string strValue = option.second;
if (InterpretNegatedOption(strKey, strValue)) {
m_config_args[strKey].clear();
} else {
m_config_args[strKey].push_back(strValue);
}
}
return true;
}
Expand Down

0 comments on commit b67fead

Please sign in to comment.