Skip to content

Commit e3ba0ef

Browse files
committed
Eliminate data races for strMiscWarning and fLargeWork*Found.
This moves all access to these datastructures through accessor functions and protects them with a lock.
1 parent c63198f commit e3ba0ef

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

src/timedata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
103103
if (!fMatch)
104104
{
105105
fDone = true;
106-
string strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), _(PACKAGE_NAME));
107-
strMiscWarning = strMessage;
106+
std::string strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), _(PACKAGE_NAME));
107+
SetMiscWarning(strMessage);
108108
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
109109
}
110110
}

src/util.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ bool fDebug = false;
108108
bool fPrintToConsole = false;
109109
bool fPrintToDebugLog = true;
110110

111+
CCriticalSection cs_warnings;
111112
string strMiscWarning;
112113
bool fLargeWorkForkFound = false;
113114
bool fLargeWorkInvalidChainFound = false;
@@ -813,13 +814,45 @@ std::string CopyrightHolders(const std::string& strPrefix)
813814
return strCopyrightHolders;
814815
}
815816

817+
void SetMiscWarning(const std::string& strWarning)
818+
{
819+
LOCK(cs_warnings);
820+
strMiscWarning = strWarning;
821+
}
822+
823+
void SetfLargeWorkForkFound(bool flag)
824+
{
825+
LOCK(cs_warnings);
826+
fLargeWorkForkFound = flag;
827+
}
828+
829+
bool GetfLargeWorkForkFound()
830+
{
831+
LOCK(cs_warnings);
832+
return fLargeWorkForkFound;
833+
}
834+
835+
void SetfLargeWorkInvalidChainFound(bool flag)
836+
{
837+
LOCK(cs_warnings);
838+
fLargeWorkInvalidChainFound = flag;
839+
}
840+
841+
bool GetfLargeWorkInvalidChainFound()
842+
{
843+
LOCK(cs_warnings);
844+
return fLargeWorkInvalidChainFound;
845+
}
846+
816847
std::string GetWarnings(const std::string& strFor)
817848
{
818849
string strStatusBar;
819850
string strRPC;
820851
string strGUI;
821852
const string uiAlertSeperator = "<hr />";
822853

854+
LOCK(cs_warnings);
855+
823856
if (!CLIENT_VERSION_IS_RELEASE) {
824857
strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
825858
strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");

src/util.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ extern bool fPrintToConsole;
4848
extern bool fPrintToDebugLog;
4949

5050
static const bool DEFAULT_TESTSAFEMODE = false;
51-
extern std::string strMiscWarning;
52-
extern bool fLargeWorkForkFound;
53-
extern bool fLargeWorkInvalidChainFound;
5451

5552
extern bool fLogTimestamps;
5653
extern bool fLogTimeMicros;
@@ -229,6 +226,11 @@ template <typename Callable> void TraceThread(const char* name, Callable func)
229226

230227
std::string CopyrightHolders(const std::string& strPrefix);
231228

229+
void SetMiscWarning(const std::string& strWarning);
230+
void SetfLargeWorkForkFound(bool flag);
231+
bool GetfLargeWorkForkFound();
232+
void SetfLargeWorkInvalidChainFound(bool flag);
233+
bool GetfLargeWorkInvalidChainFound();
232234
std::string GetWarnings(const std::string& strFor);
233235

234236
#endif // BITCOIN_UTIL_H

src/validation.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ void CheckForkWarningConditions()
11831183

11841184
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
11851185
{
1186-
if (!fLargeWorkForkFound && pindexBestForkBase)
1186+
if (!GetfLargeWorkForkFound() && pindexBestForkBase)
11871187
{
11881188
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
11891189
pindexBestForkBase->phashBlock->ToString() + std::string("'");
@@ -1194,18 +1194,18 @@ void CheckForkWarningConditions()
11941194
LogPrintf("%s: Warning: Large valid fork found\n forking the chain at height %d (%s)\n lasting to height %d (%s).\nChain state database corruption likely.\n", __func__,
11951195
pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
11961196
pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1197-
fLargeWorkForkFound = true;
1197+
SetfLargeWorkForkFound(true);
11981198
}
11991199
else
12001200
{
12011201
LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1202-
fLargeWorkInvalidChainFound = true;
1202+
SetfLargeWorkInvalidChainFound(true);
12031203
}
12041204
}
12051205
else
12061206
{
1207-
fLargeWorkForkFound = false;
1208-
fLargeWorkInvalidChainFound = false;
1207+
SetfLargeWorkForkFound(false);
1208+
SetfLargeWorkInvalidChainFound(false);
12091209
}
12101210
}
12111211

@@ -1481,7 +1481,7 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uin
14811481
/** Abort with a message */
14821482
bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
14831483
{
1484-
strMiscWarning = strMessage;
1484+
SetMiscWarning(strMessage);
14851485
LogPrintf("*** %s\n", strMessage);
14861486
uiInterface.ThreadSafeMessageBox(
14871487
userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
@@ -2050,9 +2050,10 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
20502050
ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
20512051
if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
20522052
if (state == THRESHOLD_ACTIVE) {
2053-
strMiscWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2053+
std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2054+
SetMiscWarning(strWarning);
20542055
if (!fWarned) {
2055-
AlertNotify(strMiscWarning);
2056+
AlertNotify(strWarning);
20562057
fWarned = true;
20572058
}
20582059
} else {
@@ -2072,10 +2073,11 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
20722073
warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded));
20732074
if (nUpgraded > 100/2)
20742075
{
2075-
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2076-
strMiscWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2076+
std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2077+
// notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2078+
SetMiscWarning(strWarning);
20772079
if (!fWarned) {
2078-
AlertNotify(strMiscWarning);
2080+
AlertNotify(strWarning);
20792081
fWarned = true;
20802082
}
20812083
}

0 commit comments

Comments
 (0)