Skip to content

Commit 2b5f085

Browse files
committed
Fix non-const mapMultiArgs[] access after init.
Swap mapMultiArgs for a const-reference to a _mapMultiArgs which is only accessed in util.cpp
1 parent c8042a4 commit 2b5f085

File tree

7 files changed

+56
-43
lines changed

7 files changed

+56
-43
lines changed

src/httprpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static bool multiUserAuthorized(std::string strUserPass)
9595

9696
if (mapMultiArgs.count("-rpcauth") > 0) {
9797
//Search for multi-user login/pass "rpcauth" from config
98-
BOOST_FOREACH(std::string strRPCAuth, mapMultiArgs["-rpcauth"])
98+
BOOST_FOREACH(std::string strRPCAuth, mapMultiArgs.at("-rpcauth"))
9999
{
100100
std::vector<std::string> vFields;
101101
boost::split(vFields, strRPCAuth, boost::is_any_of(":$"));

src/httpserver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static bool InitHTTPAllowList()
204204
rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet
205205
rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost
206206
if (mapMultiArgs.count("-rpcallowip")) {
207-
const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"];
207+
const std::vector<std::string>& vAllow = mapMultiArgs.at("-rpcallowip");
208208
for (std::string strAllow : vAllow) {
209209
CSubNet subnet;
210210
LookupSubNet(strAllow.c_str(), subnet);
@@ -328,8 +328,8 @@ static bool HTTPBindAddresses(struct evhttp* http)
328328
if (mapArgs.count("-rpcbind")) {
329329
LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
330330
}
331-
} else if (mapArgs.count("-rpcbind")) { // Specific bind address
332-
const std::vector<std::string>& vbind = mapMultiArgs["-rpcbind"];
331+
} else if (mapMultiArgs.count("-rpcbind")) { // Specific bind address
332+
const std::vector<std::string>& vbind = mapMultiArgs.at("-rpcbind");
333333
for (std::vector<std::string>::const_iterator i = vbind.begin(); i != vbind.end(); ++i) {
334334
int port = defaultPort;
335335
std::string host;

src/init.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ void InitParameterInteraction()
717717
LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__);
718718
}
719719

720-
if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0) {
720+
if (mapMultiArgs.count("-connect") && mapMultiArgs.at("-connect").size() > 0) {
721721
// when only connecting to trusted nodes, do not seed via DNS, or listen by default
722722
if (SoftSetBoolArg("-dnsseed", false))
723723
LogPrintf("%s: parameter interaction: -connect set -> setting -dnsseed=0\n", __func__);
@@ -880,11 +880,13 @@ bool AppInitParameterInteraction()
880880

881881
// ********************************************************* Step 3: parameter-to-internal-flags
882882

883-
fDebug = !mapMultiArgs["-debug"].empty();
883+
fDebug = mapMultiArgs.count("-debug");
884884
// Special-case: if -debug=0/-nodebug is set, turn off debugging messages
885-
const vector<string>& categories = mapMultiArgs["-debug"];
886-
if (GetBoolArg("-nodebug", false) || find(categories.begin(), categories.end(), string("0")) != categories.end())
887-
fDebug = false;
885+
if (fDebug) {
886+
const vector<string>& categories = mapMultiArgs.at("-debug");
887+
if (GetBoolArg("-nodebug", false) || find(categories.begin(), categories.end(), string("0")) != categories.end())
888+
fDebug = false;
889+
}
888890

889891
// Check for -debugnet
890892
if (GetBoolArg("-debugnet", false))
@@ -1003,12 +1005,12 @@ bool AppInitParameterInteraction()
10031005
fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end());
10041006
}
10051007

1006-
if (!mapMultiArgs["-bip9params"].empty()) {
1008+
if (mapMultiArgs.count("-bip9params")) {
10071009
// Allow overriding BIP9 parameters for testing
10081010
if (!chainparams.MineBlocksOnDemand()) {
10091011
return InitError("BIP9 parameters may only be overridden on regtest.");
10101012
}
1011-
const vector<string>& deployments = mapMultiArgs["-bip9params"];
1013+
const vector<string>& deployments = mapMultiArgs.at("-bip9params");
10121014
for (auto i : deployments) {
10131015
std::vector<std::string> vDeploymentParams;
10141016
boost::split(vDeploymentParams, i, boost::is_any_of(":"));
@@ -1154,21 +1156,23 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
11541156

11551157
// sanitize comments per BIP-0014, format user agent and check total size
11561158
std::vector<string> uacomments;
1157-
BOOST_FOREACH(string cmt, mapMultiArgs["-uacomment"])
1158-
{
1159-
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
1160-
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
1161-
uacomments.push_back(SanitizeString(cmt, SAFE_CHARS_UA_COMMENT));
1159+
if (mapMultiArgs.count("-uacomment")) {
1160+
BOOST_FOREACH(string cmt, mapMultiArgs.at("-uacomment"))
1161+
{
1162+
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
1163+
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
1164+
uacomments.push_back(cmt);
1165+
}
11621166
}
11631167
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
11641168
if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
11651169
return InitError(strprintf(_("Total length of network version string (%i) exceeds maximum length (%i). Reduce the number or size of uacomments."),
11661170
strSubVersion.size(), MAX_SUBVERSION_LENGTH));
11671171
}
11681172

1169-
if (mapArgs.count("-onlynet")) {
1173+
if (mapMultiArgs.count("-onlynet")) {
11701174
std::set<enum Network> nets;
1171-
BOOST_FOREACH(const std::string& snet, mapMultiArgs["-onlynet"]) {
1175+
BOOST_FOREACH(const std::string& snet, mapMultiArgs.at("-onlynet")) {
11721176
enum Network net = ParseNetwork(snet);
11731177
if (net == NET_UNROUTABLE)
11741178
return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet));
@@ -1181,8 +1185,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
11811185
}
11821186
}
11831187

1184-
if (mapArgs.count("-whitelist")) {
1185-
BOOST_FOREACH(const std::string& net, mapMultiArgs["-whitelist"]) {
1188+
if (mapMultiArgs.count("-whitelist")) {
1189+
BOOST_FOREACH(const std::string& net, mapMultiArgs.at("-whitelist")) {
11861190
CSubNet subnet;
11871191
LookupSubNet(net.c_str(), subnet);
11881192
if (!subnet.IsValid())
@@ -1234,14 +1238,16 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12341238

12351239
if (fListen) {
12361240
bool fBound = false;
1237-
if (mapArgs.count("-bind") || mapArgs.count("-whitebind")) {
1238-
BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-bind"]) {
1241+
if (mapMultiArgs.count("-bind")) {
1242+
BOOST_FOREACH(const std::string& strBind, mapMultiArgs.at("-bind")) {
12391243
CService addrBind;
12401244
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
12411245
return InitError(ResolveErrMsg("bind", strBind));
12421246
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
12431247
}
1244-
BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-whitebind"]) {
1248+
}
1249+
if (mapMultiArgs.count("-whitebind")) {
1250+
BOOST_FOREACH(const std::string& strBind, mapMultiArgs.at("-whitebind")) {
12451251
CService addrBind;
12461252
if (!Lookup(strBind.c_str(), addrBind, 0, false))
12471253
return InitError(ResolveErrMsg("whitebind", strBind));
@@ -1250,7 +1256,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12501256
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
12511257
}
12521258
}
1253-
else {
1259+
if (!mapMultiArgs.count("-bind") && !mapMultiArgs.count("-whitebind")) {
12541260
struct in_addr inaddr_any;
12551261
inaddr_any.s_addr = INADDR_ANY;
12561262
fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE);
@@ -1260,8 +1266,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12601266
return InitError(_("Failed to listen on any port. Use -listen=0 if you want this."));
12611267
}
12621268

1263-
if (mapArgs.count("-externalip")) {
1264-
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-externalip"]) {
1269+
if (mapMultiArgs.count("-externalip")) {
1270+
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs.at("-externalip")) {
12651271
CService addrLocal;
12661272
if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
12671273
AddLocal(addrLocal, LOCAL_MANUAL);
@@ -1270,8 +1276,10 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12701276
}
12711277
}
12721278

1273-
BOOST_FOREACH(const std::string& strDest, mapMultiArgs["-seednode"])
1274-
connman.AddOneShot(strDest);
1279+
if (mapMultiArgs.count("-seednode")) {
1280+
BOOST_FOREACH(const std::string& strDest, mapMultiArgs.at("-seednode"))
1281+
connman.AddOneShot(strDest);
1282+
}
12751283

12761284
#if ENABLE_ZMQ
12771285
pzmqNotificationInterface = CZMQNotificationInterface::CreateWithArguments(mapArgs);
@@ -1519,9 +1527,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15191527
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
15201528

15211529
std::vector<boost::filesystem::path> vImportFiles;
1522-
if (mapArgs.count("-loadblock"))
1530+
if (mapMultiArgs.count("-loadblock"))
15231531
{
1524-
BOOST_FOREACH(const std::string& strFile, mapMultiArgs["-loadblock"])
1532+
BOOST_FOREACH(const std::string& strFile, mapMultiArgs.at("-loadblock"))
15251533
vImportFiles.push_back(strFile);
15261534
}
15271535

src/net.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,12 +1569,12 @@ void CConnman::ProcessOneShot()
15691569
void CConnman::ThreadOpenConnections()
15701570
{
15711571
// Connect to specific addresses
1572-
if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
1572+
if (mapMultiArgs.count("-connect") && mapMultiArgs.at("-connect").size() > 0)
15731573
{
15741574
for (int64_t nLoop = 0;; nLoop++)
15751575
{
15761576
ProcessOneShot();
1577-
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
1577+
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs.at("-connect"))
15781578
{
15791579
CAddress addr(CService(), NODE_NONE);
15801580
OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
@@ -1765,7 +1765,8 @@ void CConnman::ThreadOpenAddedConnections()
17651765
{
17661766
{
17671767
LOCK(cs_vAddedNodes);
1768-
vAddedNodes = mapMultiArgs["-addnode"];
1768+
if (mapMultiArgs.count("-addnode"))
1769+
vAddedNodes = mapMultiArgs.at("-addnode");
17691770
}
17701771

17711772
for (unsigned int i = 0; true; i++)
@@ -2157,7 +2158,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
21572158
threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "addcon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenAddedConnections, this))));
21582159

21592160
// Initiate outbound connections unless connect=0
2160-
if (!mapArgs.count("-connect") || mapMultiArgs["-connect"].size() != 1 || mapMultiArgs["-connect"][0] != "0")
2161+
if (!mapMultiArgs.count("-connect") || mapMultiArgs.at("-connect").size() != 1 || mapMultiArgs.at("-connect")[0] != "0")
21612162
threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "opencon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenConnections, this))));
21622163

21632164
// Process messages

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
121121
&& !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
122122

123123
BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
124-
BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
124+
BOOST_CHECK(mapMultiArgs.at("-ccc").size() == 2);
125125
}
126126

127127
BOOST_AUTO_TEST_CASE(util_GetArg)

src/util.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
103103
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
104104

105105
map<string, string> mapArgs;
106-
map<string, vector<string> > mapMultiArgs;
106+
static map<string, vector<string> > _mapMultiArgs;
107+
const map<string, vector<string> >& mapMultiArgs = _mapMultiArgs;
107108
bool fDebug = false;
108109
bool fPrintToConsole = false;
109110
bool fPrintToDebugLog = true;
@@ -238,9 +239,12 @@ bool LogAcceptCategory(const char* category)
238239
static boost::thread_specific_ptr<set<string> > ptrCategory;
239240
if (ptrCategory.get() == NULL)
240241
{
241-
const vector<string>& categories = mapMultiArgs["-debug"];
242-
ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
243-
// thread_specific_ptr automatically deletes the set when the thread ends.
242+
if (mapMultiArgs.count("-debug")) {
243+
const vector<string>& categories = mapMultiArgs.at("-debug");
244+
ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
245+
// thread_specific_ptr automatically deletes the set when the thread ends.
246+
} else
247+
ptrCategory.reset(new set<string>());
244248
}
245249
const set<string>& setCategories = *ptrCategory.get();
246250

@@ -343,7 +347,7 @@ static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
343347
void ParseParameters(int argc, const char* const argv[])
344348
{
345349
mapArgs.clear();
346-
mapMultiArgs.clear();
350+
_mapMultiArgs.clear();
347351

348352
for (int i = 1; i < argc; i++)
349353
{
@@ -371,7 +375,7 @@ void ParseParameters(int argc, const char* const argv[])
371375
InterpretNegativeSetting(str, strValue);
372376

373377
mapArgs[str] = strValue;
374-
mapMultiArgs[str].push_back(strValue);
378+
_mapMultiArgs[str].push_back(strValue);
375379
}
376380
}
377381

@@ -543,7 +547,7 @@ void ReadConfigFile(const std::string& confPath)
543547
InterpretNegativeSetting(strKey, strValue);
544548
if (mapArgs.count(strKey) == 0)
545549
mapArgs[strKey] = strValue;
546-
mapMultiArgs[strKey].push_back(strValue);
550+
_mapMultiArgs[strKey].push_back(strValue);
547551
}
548552
// If datadir is changed in .conf file:
549553
ClearDatadirCache();

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class CTranslationInterface
4242
};
4343

4444
extern std::map<std::string, std::string> mapArgs;
45-
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
45+
extern const std::map<std::string, std::vector<std::string> >& mapMultiArgs;
4646
extern bool fDebug;
4747
extern bool fPrintToConsole;
4848
extern bool fPrintToDebugLog;

0 commit comments

Comments
 (0)