Browse Source

Fix non-const mapMultiArgs[] access after init.

Swap mapMultiArgs for a const-reference to a _mapMultiArgs which is
only accessed in util.cpp
0.14
Matt Corallo 8 years ago
parent
commit
2b5f085ad1
  1. 2
      src/httprpc.cpp
  2. 6
      src/httpserver.cpp
  3. 60
      src/init.cpp
  4. 9
      src/net.cpp
  5. 2
      src/test/util_tests.cpp
  6. 18
      src/util.cpp
  7. 2
      src/util.h

2
src/httprpc.cpp

@ -95,7 +95,7 @@ static bool multiUserAuthorized(std::string strUserPass)
if (mapMultiArgs.count("-rpcauth") > 0) { if (mapMultiArgs.count("-rpcauth") > 0) {
//Search for multi-user login/pass "rpcauth" from config //Search for multi-user login/pass "rpcauth" from config
BOOST_FOREACH(std::string strRPCAuth, mapMultiArgs["-rpcauth"]) BOOST_FOREACH(std::string strRPCAuth, mapMultiArgs.at("-rpcauth"))
{ {
std::vector<std::string> vFields; std::vector<std::string> vFields;
boost::split(vFields, strRPCAuth, boost::is_any_of(":$")); boost::split(vFields, strRPCAuth, boost::is_any_of(":$"));

6
src/httpserver.cpp

@ -204,7 +204,7 @@ static bool InitHTTPAllowList()
rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet
rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost
if (mapMultiArgs.count("-rpcallowip")) { if (mapMultiArgs.count("-rpcallowip")) {
const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"]; const std::vector<std::string>& vAllow = mapMultiArgs.at("-rpcallowip");
for (std::string strAllow : vAllow) { for (std::string strAllow : vAllow) {
CSubNet subnet; CSubNet subnet;
LookupSubNet(strAllow.c_str(), subnet); LookupSubNet(strAllow.c_str(), subnet);
@ -328,8 +328,8 @@ static bool HTTPBindAddresses(struct evhttp* http)
if (mapArgs.count("-rpcbind")) { if (mapArgs.count("-rpcbind")) {
LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n"); LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
} }
} else if (mapArgs.count("-rpcbind")) { // Specific bind address } else if (mapMultiArgs.count("-rpcbind")) { // Specific bind address
const std::vector<std::string>& vbind = mapMultiArgs["-rpcbind"]; const std::vector<std::string>& vbind = mapMultiArgs.at("-rpcbind");
for (std::vector<std::string>::const_iterator i = vbind.begin(); i != vbind.end(); ++i) { for (std::vector<std::string>::const_iterator i = vbind.begin(); i != vbind.end(); ++i) {
int port = defaultPort; int port = defaultPort;
std::string host; std::string host;

60
src/init.cpp

@ -717,7 +717,7 @@ void InitParameterInteraction()
LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__); LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__);
} }
if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0) { if (mapMultiArgs.count("-connect") && mapMultiArgs.at("-connect").size() > 0) {
// when only connecting to trusted nodes, do not seed via DNS, or listen by default // when only connecting to trusted nodes, do not seed via DNS, or listen by default
if (SoftSetBoolArg("-dnsseed", false)) if (SoftSetBoolArg("-dnsseed", false))
LogPrintf("%s: parameter interaction: -connect set -> setting -dnsseed=0\n", __func__); LogPrintf("%s: parameter interaction: -connect set -> setting -dnsseed=0\n", __func__);
@ -880,11 +880,13 @@ bool AppInitParameterInteraction()
// ********************************************************* Step 3: parameter-to-internal-flags // ********************************************************* Step 3: parameter-to-internal-flags
fDebug = !mapMultiArgs["-debug"].empty(); fDebug = mapMultiArgs.count("-debug");
// Special-case: if -debug=0/-nodebug is set, turn off debugging messages // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
const vector<string>& categories = mapMultiArgs["-debug"]; if (fDebug) {
if (GetBoolArg("-nodebug", false) || find(categories.begin(), categories.end(), string("0")) != categories.end()) const vector<string>& categories = mapMultiArgs.at("-debug");
fDebug = false; if (GetBoolArg("-nodebug", false) || find(categories.begin(), categories.end(), string("0")) != categories.end())
fDebug = false;
}
// Check for -debugnet // Check for -debugnet
if (GetBoolArg("-debugnet", false)) if (GetBoolArg("-debugnet", false))
@ -1003,12 +1005,12 @@ bool AppInitParameterInteraction()
fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end()); fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end());
} }
if (!mapMultiArgs["-bip9params"].empty()) { if (mapMultiArgs.count("-bip9params")) {
// Allow overriding BIP9 parameters for testing // Allow overriding BIP9 parameters for testing
if (!chainparams.MineBlocksOnDemand()) { if (!chainparams.MineBlocksOnDemand()) {
return InitError("BIP9 parameters may only be overridden on regtest."); return InitError("BIP9 parameters may only be overridden on regtest.");
} }
const vector<string>& deployments = mapMultiArgs["-bip9params"]; const vector<string>& deployments = mapMultiArgs.at("-bip9params");
for (auto i : deployments) { for (auto i : deployments) {
std::vector<std::string> vDeploymentParams; std::vector<std::string> vDeploymentParams;
boost::split(vDeploymentParams, i, boost::is_any_of(":")); boost::split(vDeploymentParams, i, boost::is_any_of(":"));
@ -1154,11 +1156,13 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
// sanitize comments per BIP-0014, format user agent and check total size // sanitize comments per BIP-0014, format user agent and check total size
std::vector<string> uacomments; std::vector<string> uacomments;
BOOST_FOREACH(string cmt, mapMultiArgs["-uacomment"]) if (mapMultiArgs.count("-uacomment")) {
{ BOOST_FOREACH(string cmt, mapMultiArgs.at("-uacomment"))
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT)) {
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt)); if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
uacomments.push_back(SanitizeString(cmt, SAFE_CHARS_UA_COMMENT)); return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
uacomments.push_back(cmt);
}
} }
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments); strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) { if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
@ -1166,9 +1170,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
strSubVersion.size(), MAX_SUBVERSION_LENGTH)); strSubVersion.size(), MAX_SUBVERSION_LENGTH));
} }
if (mapArgs.count("-onlynet")) { if (mapMultiArgs.count("-onlynet")) {
std::set<enum Network> nets; std::set<enum Network> nets;
BOOST_FOREACH(const std::string& snet, mapMultiArgs["-onlynet"]) { BOOST_FOREACH(const std::string& snet, mapMultiArgs.at("-onlynet")) {
enum Network net = ParseNetwork(snet); enum Network net = ParseNetwork(snet);
if (net == NET_UNROUTABLE) if (net == NET_UNROUTABLE)
return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet)); return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet));
@ -1181,8 +1185,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
} }
} }
if (mapArgs.count("-whitelist")) { if (mapMultiArgs.count("-whitelist")) {
BOOST_FOREACH(const std::string& net, mapMultiArgs["-whitelist"]) { BOOST_FOREACH(const std::string& net, mapMultiArgs.at("-whitelist")) {
CSubNet subnet; CSubNet subnet;
LookupSubNet(net.c_str(), subnet); LookupSubNet(net.c_str(), subnet);
if (!subnet.IsValid()) if (!subnet.IsValid())
@ -1234,14 +1238,16 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
if (fListen) { if (fListen) {
bool fBound = false; bool fBound = false;
if (mapArgs.count("-bind") || mapArgs.count("-whitebind")) { if (mapMultiArgs.count("-bind")) {
BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-bind"]) { BOOST_FOREACH(const std::string& strBind, mapMultiArgs.at("-bind")) {
CService addrBind; CService addrBind;
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
return InitError(ResolveErrMsg("bind", strBind)); return InitError(ResolveErrMsg("bind", strBind));
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR)); fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
} }
BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-whitebind"]) { }
if (mapMultiArgs.count("-whitebind")) {
BOOST_FOREACH(const std::string& strBind, mapMultiArgs.at("-whitebind")) {
CService addrBind; CService addrBind;
if (!Lookup(strBind.c_str(), addrBind, 0, false)) if (!Lookup(strBind.c_str(), addrBind, 0, false))
return InitError(ResolveErrMsg("whitebind", strBind)); return InitError(ResolveErrMsg("whitebind", strBind));
@ -1250,7 +1256,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST)); fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
} }
} }
else { if (!mapMultiArgs.count("-bind") && !mapMultiArgs.count("-whitebind")) {
struct in_addr inaddr_any; struct in_addr inaddr_any;
inaddr_any.s_addr = INADDR_ANY; inaddr_any.s_addr = INADDR_ANY;
fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE); fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE);
@ -1260,8 +1266,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
return InitError(_("Failed to listen on any port. Use -listen=0 if you want this.")); return InitError(_("Failed to listen on any port. Use -listen=0 if you want this."));
} }
if (mapArgs.count("-externalip")) { if (mapMultiArgs.count("-externalip")) {
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-externalip"]) { BOOST_FOREACH(const std::string& strAddr, mapMultiArgs.at("-externalip")) {
CService addrLocal; CService addrLocal;
if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid()) if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
AddLocal(addrLocal, LOCAL_MANUAL); AddLocal(addrLocal, LOCAL_MANUAL);
@ -1270,8 +1276,10 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
} }
} }
BOOST_FOREACH(const std::string& strDest, mapMultiArgs["-seednode"]) if (mapMultiArgs.count("-seednode")) {
connman.AddOneShot(strDest); BOOST_FOREACH(const std::string& strDest, mapMultiArgs.at("-seednode"))
connman.AddOneShot(strDest);
}
#if ENABLE_ZMQ #if ENABLE_ZMQ
pzmqNotificationInterface = CZMQNotificationInterface::CreateWithArguments(mapArgs); pzmqNotificationInterface = CZMQNotificationInterface::CreateWithArguments(mapArgs);
@ -1519,9 +1527,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback); uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
std::vector<boost::filesystem::path> vImportFiles; std::vector<boost::filesystem::path> vImportFiles;
if (mapArgs.count("-loadblock")) if (mapMultiArgs.count("-loadblock"))
{ {
BOOST_FOREACH(const std::string& strFile, mapMultiArgs["-loadblock"]) BOOST_FOREACH(const std::string& strFile, mapMultiArgs.at("-loadblock"))
vImportFiles.push_back(strFile); vImportFiles.push_back(strFile);
} }

9
src/net.cpp

@ -1569,12 +1569,12 @@ void CConnman::ProcessOneShot()
void CConnman::ThreadOpenConnections() void CConnman::ThreadOpenConnections()
{ {
// Connect to specific addresses // Connect to specific addresses
if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0) if (mapMultiArgs.count("-connect") && mapMultiArgs.at("-connect").size() > 0)
{ {
for (int64_t nLoop = 0;; nLoop++) for (int64_t nLoop = 0;; nLoop++)
{ {
ProcessOneShot(); ProcessOneShot();
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"]) BOOST_FOREACH(const std::string& strAddr, mapMultiArgs.at("-connect"))
{ {
CAddress addr(CService(), NODE_NONE); CAddress addr(CService(), NODE_NONE);
OpenNetworkConnection(addr, false, NULL, strAddr.c_str()); OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
@ -1765,7 +1765,8 @@ void CConnman::ThreadOpenAddedConnections()
{ {
{ {
LOCK(cs_vAddedNodes); LOCK(cs_vAddedNodes);
vAddedNodes = mapMultiArgs["-addnode"]; if (mapMultiArgs.count("-addnode"))
vAddedNodes = mapMultiArgs.at("-addnode");
} }
for (unsigned int i = 0; true; i++) for (unsigned int i = 0; true; i++)
@ -2157,7 +2158,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "addcon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenAddedConnections, this)))); threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "addcon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenAddedConnections, this))));
// Initiate outbound connections unless connect=0 // Initiate outbound connections unless connect=0
if (!mapArgs.count("-connect") || mapMultiArgs["-connect"].size() != 1 || mapMultiArgs["-connect"][0] != "0") if (!mapMultiArgs.count("-connect") || mapMultiArgs.at("-connect").size() != 1 || mapMultiArgs.at("-connect")[0] != "0")
threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "opencon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenConnections, this)))); threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "opencon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenConnections, this))));
// Process messages // Process messages

2
src/test/util_tests.cpp

@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
&& !mapMultiArgs.count("f") && !mapMultiArgs.count("-d")); && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple"); BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2); BOOST_CHECK(mapMultiArgs.at("-ccc").size() == 2);
} }
BOOST_AUTO_TEST_CASE(util_GetArg) BOOST_AUTO_TEST_CASE(util_GetArg)

18
src/util.cpp

@ -103,7 +103,8 @@ const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid"; const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
map<string, string> mapArgs; map<string, string> mapArgs;
map<string, vector<string> > mapMultiArgs; static map<string, vector<string> > _mapMultiArgs;
const map<string, vector<string> >& mapMultiArgs = _mapMultiArgs;
bool fDebug = false; bool fDebug = false;
bool fPrintToConsole = false; bool fPrintToConsole = false;
bool fPrintToDebugLog = true; bool fPrintToDebugLog = true;
@ -238,9 +239,12 @@ bool LogAcceptCategory(const char* category)
static boost::thread_specific_ptr<set<string> > ptrCategory; static boost::thread_specific_ptr<set<string> > ptrCategory;
if (ptrCategory.get() == NULL) if (ptrCategory.get() == NULL)
{ {
const vector<string>& categories = mapMultiArgs["-debug"]; if (mapMultiArgs.count("-debug")) {
ptrCategory.reset(new set<string>(categories.begin(), categories.end())); const vector<string>& categories = mapMultiArgs.at("-debug");
// thread_specific_ptr automatically deletes the set when the thread ends. ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
// thread_specific_ptr automatically deletes the set when the thread ends.
} else
ptrCategory.reset(new set<string>());
} }
const set<string>& setCategories = *ptrCategory.get(); const set<string>& setCategories = *ptrCategory.get();
@ -343,7 +347,7 @@ static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
void ParseParameters(int argc, const char* const argv[]) void ParseParameters(int argc, const char* const argv[])
{ {
mapArgs.clear(); mapArgs.clear();
mapMultiArgs.clear(); _mapMultiArgs.clear();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
@ -371,7 +375,7 @@ void ParseParameters(int argc, const char* const argv[])
InterpretNegativeSetting(str, strValue); InterpretNegativeSetting(str, strValue);
mapArgs[str] = strValue; mapArgs[str] = strValue;
mapMultiArgs[str].push_back(strValue); _mapMultiArgs[str].push_back(strValue);
} }
} }
@ -543,7 +547,7 @@ void ReadConfigFile(const std::string& confPath)
InterpretNegativeSetting(strKey, strValue); InterpretNegativeSetting(strKey, strValue);
if (mapArgs.count(strKey) == 0) if (mapArgs.count(strKey) == 0)
mapArgs[strKey] = strValue; mapArgs[strKey] = strValue;
mapMultiArgs[strKey].push_back(strValue); _mapMultiArgs[strKey].push_back(strValue);
} }
// If datadir is changed in .conf file: // If datadir is changed in .conf file:
ClearDatadirCache(); ClearDatadirCache();

2
src/util.h

@ -42,7 +42,7 @@ public:
}; };
extern std::map<std::string, std::string> mapArgs; extern std::map<std::string, std::string> mapArgs;
extern std::map<std::string, std::vector<std::string> > mapMultiArgs; extern const std::map<std::string, std::vector<std::string> >& mapMultiArgs;
extern bool fDebug; extern bool fDebug;
extern bool fPrintToConsole; extern bool fPrintToConsole;
extern bool fPrintToDebugLog; extern bool fPrintToDebugLog;

Loading…
Cancel
Save