Skip to content

Commit ac52492

Browse files
committed
Merge #10118: Util: Remove redundant calls to argsGlobal.IsArgSet()
ed866ab Indentation after 'Remove redundant calls to gArgs.IsArgSet()' (Jorge Timón) 506b700 Util: Remove redundant calls to gArgs.IsArgSet() (Jorge Timón) Tree-SHA512: 4f97a0bf2a76c0f351a6343db62898cf057d745c848de00fa09465e870a120f28e0d836cafd6a047f4ec0da7ab671aebee43fa7410c9f0e66382edd1bb2009ba
2 parents 7c87a9c + ed866ab commit ac52492

File tree

5 files changed

+77
-98
lines changed

5 files changed

+77
-98
lines changed

src/httprpc.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,32 @@ static bool multiUserAuthorized(std::string strUserPass)
9292
std::string strUser = strUserPass.substr(0, strUserPass.find(":"));
9393
std::string strPass = strUserPass.substr(strUserPass.find(":") + 1);
9494

95-
if (gArgs.IsArgSet("-rpcauth")) {
95+
for (const std::string& strRPCAuth : gArgs.GetArgs("-rpcauth")) {
9696
//Search for multi-user login/pass "rpcauth" from config
97-
for (std::string strRPCAuth : gArgs.GetArgs("-rpcauth"))
98-
{
99-
std::vector<std::string> vFields;
100-
boost::split(vFields, strRPCAuth, boost::is_any_of(":$"));
101-
if (vFields.size() != 3) {
102-
//Incorrect formatting in config file
103-
continue;
104-
}
105-
106-
std::string strName = vFields[0];
107-
if (!TimingResistantEqual(strName, strUser)) {
108-
continue;
109-
}
110-
111-
std::string strSalt = vFields[1];
112-
std::string strHash = vFields[2];
113-
114-
static const unsigned int KEY_SIZE = 32;
115-
unsigned char out[KEY_SIZE];
116-
117-
CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.c_str()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.c_str()), strPass.size()).Finalize(out);
118-
std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
119-
std::string strHashFromPass = HexStr(hexvec);
120-
121-
if (TimingResistantEqual(strHashFromPass, strHash)) {
122-
return true;
123-
}
97+
std::vector<std::string> vFields;
98+
boost::split(vFields, strRPCAuth, boost::is_any_of(":$"));
99+
if (vFields.size() != 3) {
100+
//Incorrect formatting in config file
101+
continue;
102+
}
103+
104+
std::string strName = vFields[0];
105+
if (!TimingResistantEqual(strName, strUser)) {
106+
continue;
107+
}
108+
109+
std::string strSalt = vFields[1];
110+
std::string strHash = vFields[2];
111+
112+
static const unsigned int KEY_SIZE = 32;
113+
unsigned char out[KEY_SIZE];
114+
115+
CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.c_str()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.c_str()), strPass.size()).Finalize(out);
116+
std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
117+
std::string strHashFromPass = HexStr(hexvec);
118+
119+
if (TimingResistantEqual(strHashFromPass, strHash)) {
120+
return true;
124121
}
125122
}
126123
return false;

src/httpserver.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,16 @@ static bool InitHTTPAllowList()
196196
LookupHost("::1", localv6, false);
197197
rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet
198198
rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost
199-
if (gArgs.IsArgSet("-rpcallowip")) {
200-
for (const std::string& strAllow : gArgs.GetArgs("-rpcallowip")) {
201-
CSubNet subnet;
202-
LookupSubNet(strAllow.c_str(), subnet);
203-
if (!subnet.IsValid()) {
204-
uiInterface.ThreadSafeMessageBox(
205-
strprintf("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24).", strAllow),
206-
"", CClientUIInterface::MSG_ERROR);
207-
return false;
208-
}
209-
rpc_allow_subnets.push_back(subnet);
199+
for (const std::string& strAllow : gArgs.GetArgs("-rpcallowip")) {
200+
CSubNet subnet;
201+
LookupSubNet(strAllow.c_str(), subnet);
202+
if (!subnet.IsValid()) {
203+
uiInterface.ThreadSafeMessageBox(
204+
strprintf("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24).", strAllow),
205+
"", CClientUIInterface::MSG_ERROR);
206+
return false;
210207
}
208+
rpc_allow_subnets.push_back(subnet);
211209
}
212210
std::string strAllowed;
213211
for (const CSubNet& subnet : rpc_allow_subnets)

src/init.cpp

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,7 @@ bool AppInitParameterInteraction()
880880
}
881881

882882
// -bind and -whitebind can't be set when not listening
883-
size_t nUserBind =
884-
(gArgs.IsArgSet("-bind") ? gArgs.GetArgs("-bind").size() : 0) +
885-
(gArgs.IsArgSet("-whitebind") ? gArgs.GetArgs("-whitebind").size() : 0);
883+
size_t nUserBind = gArgs.GetArgs("-bind").size() + gArgs.GetArgs("-whitebind").size();
886884
if (nUserBind != 0 && !gArgs.GetBoolArg("-listen", DEFAULT_LISTEN)) {
887885
return InitError("Cannot set -bind or -whitebind together with -listen=0");
888886
}
@@ -920,15 +918,13 @@ bool AppInitParameterInteraction()
920918
}
921919

922920
// Now remove the logging categories which were explicitly excluded
923-
if (gArgs.IsArgSet("-debugexclude")) {
924-
for (const std::string& cat : gArgs.GetArgs("-debugexclude")) {
925-
uint32_t flag = 0;
926-
if (!GetLogCategory(&flag, &cat)) {
927-
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
928-
continue;
929-
}
930-
logCategories &= ~flag;
921+
for (const std::string& cat : gArgs.GetArgs("-debugexclude")) {
922+
uint32_t flag = 0;
923+
if (!GetLogCategory(&flag, &cat)) {
924+
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
925+
continue;
931926
}
927+
logCategories &= ~flag;
932928
}
933929

934930
// Check for -debugnet
@@ -1238,13 +1234,10 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12381234

12391235
// sanitize comments per BIP-0014, format user agent and check total size
12401236
std::vector<std::string> uacomments;
1241-
if (gArgs.IsArgSet("-uacomment")) {
1242-
for (std::string cmt : gArgs.GetArgs("-uacomment"))
1243-
{
1244-
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
1245-
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
1246-
uacomments.push_back(cmt);
1247-
}
1237+
for (const std::string& cmt : gArgs.GetArgs("-uacomment")) {
1238+
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
1239+
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
1240+
uacomments.push_back(cmt);
12481241
}
12491242
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
12501243
if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
@@ -1317,14 +1310,12 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13171310
fDiscover = GetBoolArg("-discover", true);
13181311
fRelayTxes = !GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY);
13191312

1320-
if (gArgs.IsArgSet("-externalip")) {
1321-
for (const std::string& strAddr : gArgs.GetArgs("-externalip")) {
1322-
CService addrLocal;
1323-
if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
1324-
AddLocal(addrLocal, LOCAL_MANUAL);
1325-
else
1326-
return InitError(ResolveErrMsg("externalip", strAddr));
1327-
}
1313+
for (const std::string& strAddr : gArgs.GetArgs("-externalip")) {
1314+
CService addrLocal;
1315+
if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
1316+
AddLocal(addrLocal, LOCAL_MANUAL);
1317+
else
1318+
return InitError(ResolveErrMsg("externalip", strAddr));
13281319
}
13291320

13301321
#if ENABLE_ZMQ
@@ -1553,10 +1544,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15531544
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
15541545

15551546
std::vector<fs::path> vImportFiles;
1556-
if (gArgs.IsArgSet("-loadblock"))
1557-
{
1558-
for (const std::string& strFile : gArgs.GetArgs("-loadblock"))
1559-
vImportFiles.push_back(strFile);
1547+
for (const std::string& strFile : gArgs.GetArgs("-loadblock")) {
1548+
vImportFiles.push_back(strFile);
15601549
}
15611550

15621551
threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles));
@@ -1598,36 +1587,30 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15981587
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
15991588
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
16001589

1601-
if (gArgs.IsArgSet("-bind")) {
1602-
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1603-
CService addrBind;
1604-
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) {
1605-
return InitError(ResolveErrMsg("bind", strBind));
1606-
}
1607-
connOptions.vBinds.push_back(addrBind);
1590+
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1591+
CService addrBind;
1592+
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) {
1593+
return InitError(ResolveErrMsg("bind", strBind));
16081594
}
1595+
connOptions.vBinds.push_back(addrBind);
16091596
}
1610-
if (gArgs.IsArgSet("-whitebind")) {
1611-
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1612-
CService addrBind;
1613-
if (!Lookup(strBind.c_str(), addrBind, 0, false)) {
1614-
return InitError(ResolveErrMsg("whitebind", strBind));
1615-
}
1616-
if (addrBind.GetPort() == 0) {
1617-
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
1618-
}
1619-
connOptions.vWhiteBinds.push_back(addrBind);
1597+
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1598+
CService addrBind;
1599+
if (!Lookup(strBind.c_str(), addrBind, 0, false)) {
1600+
return InitError(ResolveErrMsg("whitebind", strBind));
1601+
}
1602+
if (addrBind.GetPort() == 0) {
1603+
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
16201604
}
1605+
connOptions.vWhiteBinds.push_back(addrBind);
16211606
}
16221607

1623-
if (gArgs.IsArgSet("-whitelist")) {
1624-
for (const auto& net : gArgs.GetArgs("-whitelist")) {
1625-
CSubNet subnet;
1626-
LookupSubNet(net.c_str(), subnet);
1627-
if (!subnet.IsValid())
1628-
return InitError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
1629-
connOptions.vWhitelistedRange.push_back(subnet);
1630-
}
1608+
for (const auto& net : gArgs.GetArgs("-whitelist")) {
1609+
CSubNet subnet;
1610+
LookupSubNet(net.c_str(), subnet);
1611+
if (!subnet.IsValid())
1612+
return InitError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
1613+
connOptions.vWhitelistedRange.push_back(subnet);
16311614
}
16321615

16331616
if (gArgs.IsArgSet("-seednode")) {

src/net.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ void CConnman::ProcessOneShot()
16771677
void CConnman::ThreadOpenConnections()
16781678
{
16791679
// Connect to specific addresses
1680-
if (gArgs.IsArgSet("-connect") && gArgs.GetArgs("-connect").size() > 0)
1680+
if (gArgs.IsArgSet("-connect"))
16811681
{
16821682
for (int64_t nLoop = 0;; nLoop++)
16831683
{
@@ -1903,8 +1903,7 @@ void CConnman::ThreadOpenAddedConnections()
19031903
{
19041904
{
19051905
LOCK(cs_vAddedNodes);
1906-
if (gArgs.IsArgSet("-addnode"))
1907-
vAddedNodes = gArgs.GetArgs("-addnode");
1906+
vAddedNodes = gArgs.GetArgs("-addnode");
19081907
}
19091908

19101909
while (true)

src/util.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ void ArgsManager::ParseParameters(int argc, const char* const argv[])
422422
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg)
423423
{
424424
LOCK(cs_args);
425-
return mapMultiArgs.at(strArg);
425+
if (IsArgSet(strArg))
426+
return mapMultiArgs.at(strArg);
427+
return {};
426428
}
427429

428430
bool ArgsManager::IsArgSet(const std::string& strArg)

0 commit comments

Comments
 (0)