Skip to content

Commit a9b6ba0

Browse files
Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
1 parent 0cc9876 commit a9b6ba0

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/rest.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ static bool rest_headers(HTTPRequest* req,
178178
}
179179
case RF_JSON: {
180180
UniValue jsonHeaders(UniValue::VARR);
181-
for (const CBlockIndex *pindex : headers) {
182-
jsonHeaders.push_back(blockheaderToJSON(pindex));
181+
{
182+
LOCK(cs_main);
183+
for (const CBlockIndex *pindex : headers) {
184+
jsonHeaders.push_back(blockheaderToJSON(pindex));
185+
}
183186
}
184187
std::string strJSON = jsonHeaders.write() + "\n";
185188
req->WriteHeader("Content-Type", "application/json");
@@ -239,7 +242,11 @@ static bool rest_block(HTTPRequest* req,
239242
}
240243

241244
case RF_JSON: {
242-
UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
245+
UniValue objBlock;
246+
{
247+
LOCK(cs_main);
248+
objBlock = blockToJSON(block, pblockindex, showTxDetails);
249+
}
243250
std::string strJSON = objBlock.write() + "\n";
244251
req->WriteHeader("Content-Type", "application/json");
245252
req->WriteReply(HTTP_OK, strJSON);

src/rpc/blockchain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ double GetDifficulty(const CBlockIndex* blockindex)
7878

7979
UniValue blockheaderToJSON(const CBlockIndex* blockindex)
8080
{
81+
AssertLockHeld(cs_main);
8182
UniValue result(UniValue::VOBJ);
8283
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
8384
int confirmations = -1;
@@ -106,6 +107,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
106107

107108
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails)
108109
{
110+
AssertLockHeld(cs_main);
109111
UniValue result(UniValue::VOBJ);
110112
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
111113
int confirmations = -1;

0 commit comments

Comments
 (0)