Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: Fix crash when parsing command line with -noincludeconf=0, Properly handle -noincludeconf on command line #2473

Merged
merged 4 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

#include <vector>
#include <boost/test/unit_test.hpp>
#include "univalue/include/univalue.h"

#include <main.h>
#include <wallet/wallet.h>
#include <util.h>
//#include <util/system.h>
#include <util/settings.h>

#include <cstdint>

Expand Down Expand Up @@ -145,6 +148,165 @@ BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
*/
}

struct TestArgsManager : public ArgsManager
{
TestArgsManager() { m_network_only_args.clear(); }
void ReadConfigString(const std::string str_config)
{
std::istringstream streamConfig(str_config);
{
LOCK(cs_args);
m_settings.ro_config.clear();
m_config_sections.clear();
}
std::string error;
BOOST_REQUIRE(ReadConfigStream(streamConfig, "", error));
}
void SetNetworkOnlyArg(const std::string arg)
{
LOCK(cs_args);
m_network_only_args.insert(arg);
}
void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
{
for (const auto& arg : args) {
AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
}
}
using ArgsManager::GetSetting;
using ArgsManager::GetSettingsList;
using ArgsManager::ReadConfigStream;
using ArgsManager::cs_args;
using ArgsManager::m_network;
using ArgsManager::m_settings;
};

/* This is reserved when we port over the TestChain 100 block setup class from Bitcoin.
//! Test GetSetting and GetArg type coercion, negation, and default value handling.
class CheckValueTest : public TestChain100Setup
{
public:
struct Expect {
util::SettingsValue setting;
bool default_string = false;
bool default_int = false;
bool default_bool = false;
const char* string_value = nullptr;
std::optional<int64_t> int_value;
std::optional<bool> bool_value;
std::optional<std::vector<std::string>> list_value;
const char* error = nullptr;

explicit Expect(util::SettingsValue s) : setting(std::move(s)) {}
Expect& DefaultString() { default_string = true; return *this; }
Expect& DefaultInt() { default_int = true; return *this; }
Expect& DefaultBool() { default_bool = true; return *this; }
Expect& String(const char* s) { string_value = s; return *this; }
Expect& Int(int64_t i) { int_value = i; return *this; }
Expect& Bool(bool b) { bool_value = b; return *this; }
Expect& List(std::vector<std::string> m) { list_value = std::move(m); return *this; }
Expect& Error(const char* e) { error = e; return *this; }
};

void CheckValue(unsigned int flags, const char* arg, const Expect& expect)
{
TestArgsManager test;
test.SetupArgs({{"-value", flags}});
const char* argv[] = {"ignored", arg};
std::string error;
bool success = test.ParseParameters(arg ? 2 : 1, (char**)argv, error);

BOOST_CHECK_EQUAL(test.GetSetting("-value").write(), expect.setting.write());
auto settings_list = test.GetSettingsList("-value");
if (expect.setting.isNull() || expect.setting.isFalse()) {
BOOST_CHECK_EQUAL(settings_list.size(), 0U);
} else {
BOOST_CHECK_EQUAL(settings_list.size(), 1U);
BOOST_CHECK_EQUAL(settings_list[0].write(), expect.setting.write());
}

if (expect.error) {
BOOST_CHECK(!success);
BOOST_CHECK_NE(error.find(expect.error), std::string::npos);
} else {
BOOST_CHECK(success);
BOOST_CHECK_EQUAL(error, "");
}

if (expect.default_string) {
BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), "zzzzz");
} else if (expect.string_value) {
BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), expect.string_value);
} else {
BOOST_CHECK(!success);
}

if (expect.default_int) {
BOOST_CHECK_EQUAL(test.GetIntArg("-value", 99999), 99999);
} else if (expect.int_value) {
BOOST_CHECK_EQUAL(test.GetIntArg("-value", 99999), *expect.int_value);
} else {
BOOST_CHECK(!success);
}

if (expect.default_bool) {
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), false);
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), true);
} else if (expect.bool_value) {
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), *expect.bool_value);
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), *expect.bool_value);
} else {
BOOST_CHECK(!success);
}

if (expect.list_value) {
auto l = test.GetArgs("-value");
BOOST_CHECK_EQUAL_COLLECTIONS(l.begin(), l.end(), expect.list_value->begin(), expect.list_value->end());
} else {
BOOST_CHECK(!success);
}
}
};

BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
{
using M = ArgsManager;

CheckValue(M::ALLOW_ANY, nullptr, Expect{{}}.DefaultString().DefaultInt().DefaultBool().List({}));
CheckValue(M::ALLOW_ANY, "-novalue", Expect{false}.String("0").Int(0).Bool(false).List({}));
CheckValue(M::ALLOW_ANY, "-novalue=", Expect{false}.String("0").Int(0).Bool(false).List({}));
CheckValue(M::ALLOW_ANY, "-novalue=0", Expect{true}.String("1").Int(1).Bool(true).List({"1"}));
CheckValue(M::ALLOW_ANY, "-novalue=1", Expect{false}.String("0").Int(0).Bool(false).List({}));
CheckValue(M::ALLOW_ANY, "-novalue=2", Expect{false}.String("0").Int(0).Bool(false).List({}));
CheckValue(M::ALLOW_ANY, "-novalue=abc", Expect{true}.String("1").Int(1).Bool(true).List({"1"}));
CheckValue(M::ALLOW_ANY, "-value", Expect{""}.String("").Int(0).Bool(true).List({""}));
CheckValue(M::ALLOW_ANY, "-value=", Expect{""}.String("").Int(0).Bool(true).List({""}));
CheckValue(M::ALLOW_ANY, "-value=0", Expect{"0"}.String("0").Int(0).Bool(false).List({"0"}));
CheckValue(M::ALLOW_ANY, "-value=1", Expect{"1"}.String("1").Int(1).Bool(true).List({"1"}));
CheckValue(M::ALLOW_ANY, "-value=2", Expect{"2"}.String("2").Int(2).Bool(true).List({"2"}));
CheckValue(M::ALLOW_ANY, "-value=abc", Expect{"abc"}.String("abc").Int(0).Bool(false).List({"abc"}));
}
*/

struct NoIncludeConfTest {
std::string Parse(const char* arg)
{
TestArgsManager test;
test.SetupArgs({{"-includeconf", ArgsManager::ALLOW_ANY}});
std::array argv{"ignored", arg};
std::string error;
(void)test.ParseParameters(argv.size(), argv.data(), error);
return error;
}
};

BOOST_FIXTURE_TEST_CASE(util_NoIncludeConf, NoIncludeConfTest)
{
BOOST_CHECK_EQUAL(Parse("-noincludeconf"), "");
BOOST_CHECK_EQUAL(Parse("-includeconf"), "-includeconf cannot be used from commandline; -includeconf=\"\"");
BOOST_CHECK_EQUAL(Parse("-includeconf=file"), "-includeconf cannot be used from commandline; -includeconf=\"file\"");
}

BOOST_AUTO_TEST_CASE(util_ParseParameters)
{
const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
Expand Down
18 changes: 12 additions & 6 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,16 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
m_settings.command_line_options[key].push_back(value);
}

// we do not allow -includeconf from command line
bool success = true;
// we do not allow -includeconf from command line, only -noincludeconf
if (auto* includes = util::FindKey(m_settings.command_line_options, "includeconf")) {
for (const auto& include : util::SettingsSpan(*includes)) {
error += "-includeconf cannot be used from commandline; -includeconf=" + include.get_str() + "\n";
success = false;
const util::SettingsSpan values{*includes};
// Range may be empty if -noincludeconf was passed
if (!values.empty()) {
error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
return false; // pick first value as example
}
}
return success;
return true;
}

std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
Expand Down Expand Up @@ -493,6 +494,11 @@ std::string ArgsManager::GetArg(const std::string& strArg, const std::string& st
return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str();
}

int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const
{
return GetArg(strArg, nDefault);
}

int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
{
const util::SettingsValue value = GetSetting(strArg);
Expand Down
9 changes: 9 additions & 0 deletions src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ class ArgsManager
*/
int64_t GetArg(const std::string& strArg, int64_t nDefault) const;

/**
* Return integer argument or default value (alias for GetArg used in newer Bitcoin code)
*
* @param strArg Argument to get (e.g. "-foo")
* @param nDefault (e.g. 1)
* @return command-line argument (0 if invalid number) or default value
*/
int64_t GetIntArg(const std::string& strArg, int64_t nDefault) const;

/**
* Return boolean argument or default value
*
Expand Down