Skip to content

Commit 6fdb319

Browse files
committed
Merge #9743: Fix several potential issues found by sanitizers
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille) 321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille) Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
2 parents bd9ec0e + 1d31093 commit 6fdb319

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/bitcoin-tx.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,13 @@ static void MutateTx(CMutableTransaction& tx, const std::string& command,
657657
MutateTxDelOutput(tx, commandVal);
658658
else if (command == "outaddr")
659659
MutateTxAddOutAddr(tx, commandVal);
660-
else if (command == "outpubkey")
660+
else if (command == "outpubkey") {
661+
if (!ecc) { ecc.reset(new Secp256k1Init()); }
661662
MutateTxAddOutPubKey(tx, commandVal);
662-
else if (command == "outmultisig")
663+
} else if (command == "outmultisig") {
664+
if (!ecc) { ecc.reset(new Secp256k1Init()); }
663665
MutateTxAddOutMultiSig(tx, commandVal);
664-
else if (command == "outscript")
666+
} else if (command == "outscript")
665667
MutateTxAddOutScript(tx, commandVal);
666668
else if (command == "outdata")
667669
MutateTxAddOutData(tx, commandVal);

src/utiltime.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
#include "utiltime.h"
1111

12+
#include <atomic>
13+
1214
#include <boost/date_time/posix_time/posix_time.hpp>
1315
#include <boost/thread.hpp>
1416

15-
static int64_t nMockTime = 0; //!< For unit testing
17+
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
1618

1719
int64_t GetTime()
1820
{
19-
if (nMockTime) return nMockTime;
21+
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
22+
if (mocktime) return mocktime;
2023

2124
time_t now = time(NULL);
2225
assert(now > 0);
@@ -25,7 +28,7 @@ int64_t GetTime()
2528

2629
void SetMockTime(int64_t nMockTimeIn)
2730
{
28-
nMockTime = nMockTimeIn;
31+
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
2932
}
3033

3134
int64_t GetTimeMillis()
@@ -52,7 +55,8 @@ int64_t GetSystemTimeInSeconds()
5255
/** Return a time useful for the debug log */
5356
int64_t GetLogTimeMicros()
5457
{
55-
if (nMockTime) return nMockTime*1000000;
58+
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
59+
if (mocktime) return mocktime*1000000;
5660

5761
return GetTimeMicros();
5862
}

0 commit comments

Comments
 (0)