Skip to content

Commit

Permalink
Lock mapArgs/mapMultiArgs access in util
Browse files Browse the repository at this point in the history
zcash: cherry picked from commit 4e04814
zcash: bitcoin/bitcoin#9243
  • Loading branch information
TheBlueMatt authored and LarryRuane committed Apr 1, 2021
1 parent cafc3f8 commit f60964c
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ using namespace std;
const char * const BITCOIN_CONF_FILENAME = "zcash.conf";
const char * const BITCOIN_PID_FILENAME = "zcashd.pid";

CCriticalSection cs_args;
map<string, string> mapArgs;
map<string, vector<string> > mapMultiArgs;
bool fDebug = false;
Expand All @@ -113,6 +114,7 @@ static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)

void ParseParameters(int argc, const char* const argv[])
{
LOCK(cs_args);
mapArgs.clear();
mapMultiArgs.clear();

Expand Down Expand Up @@ -148,27 +150,31 @@ void ParseParameters(int argc, const char* const argv[])

std::string GetArg(const std::string& strArg, const std::string& strDefault)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return mapArgs[strArg];
return strDefault;
}

int64_t GetArg(const std::string& strArg, int64_t nDefault)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return atoi64(mapArgs[strArg]);
return nDefault;
}

bool GetBoolArg(const std::string& strArg, bool fDefault)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return InterpretBool(mapArgs[strArg]);
return fDefault;
}

bool SoftSetArg(const std::string& strArg, const std::string& strValue)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return false;
mapArgs[strArg] = strValue;
Expand Down Expand Up @@ -286,7 +292,7 @@ static fs::path ZC_GetDefaultBaseParamsDir()

const fs::path &ZC_GetParamsDir()
{
LOCK(csPathCached); // Reuse the same lock as upstream.
LOCK2(cs_args, csPathCached);

fs::path &path = zc_paramsPathCached;

Expand All @@ -313,6 +319,7 @@ const fs::path &ZC_GetParamsDir()
const fs::path GetExportDir()
{
fs::path path;
LOCK(cs_args);
if (mapArgs.count("-exportdir")) {
path = fs::system_complete(mapArgs["-exportdir"]);
if (fs::exists(path) && !fs::is_directory(path)) {
Expand All @@ -328,8 +335,7 @@ const fs::path GetExportDir()

const fs::path &GetDataDir(bool fNetSpecific)
{

LOCK(csPathCached);
LOCK2(cs_args, csPathCached);

fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;

Expand Down Expand Up @@ -357,6 +363,7 @@ const fs::path &GetDataDir(bool fNetSpecific)

void ClearDatadirCache()
{
LOCK(csPathCached);
pathCached = fs::path();
pathCachedNetSpecific = fs::path();
}
Expand Down Expand Up @@ -402,23 +409,26 @@ void ReadConfigFile(const std::string& confPath,
};
set<string> unique_options;

for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
{
string strKey = string("-") + it->string_key;
string strValue = it->value[0];

if (find(allowed_duplicates.begin(), allowed_duplicates.end(), it->string_key) == allowed_duplicates.end())
LOCK(cs_args);
for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
{
if (!unique_options.insert(strKey).second) {
throw std::runtime_error(strprintf("Option '%s' is duplicated, which is not allowed.", strKey));
string strKey = string("-") + it->string_key;
string strValue = it->value[0];

if (find(allowed_duplicates.begin(), allowed_duplicates.end(), it->string_key) == allowed_duplicates.end())
{
if (!unique_options.insert(strKey).second) {
throw std::runtime_error(strprintf("Option '%s' is duplicated, which is not allowed.", strKey));
}
}
}

InterpretNegativeSetting(strKey, strValue);
// Don't overwrite existing settings so command line settings override zcash.conf
if (mapSettingsRet.count(strKey) == 0)
mapSettingsRet[strKey] = strValue;
mapMultiSettingsRet[strKey].push_back(strValue);
InterpretNegativeSetting(strKey, strValue);
// Don't overwrite existing settings so command line settings override zcash.conf
if (mapSettingsRet.count(strKey) == 0)
mapSettingsRet[strKey] = strValue;
mapMultiSettingsRet[strKey].push_back(strValue);
}
}
// If datadir is changed in .conf file:
ClearDatadirCache();
Expand Down

0 comments on commit f60964c

Please sign in to comment.