Skip to content

Commit

Permalink
Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalswift committed Nov 7, 2017
1 parent 0cc9876 commit a9b6ba0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/rest.cpp
Expand Up @@ -178,8 +178,11 @@ static bool rest_headers(HTTPRequest* req,
}
case RF_JSON: {
UniValue jsonHeaders(UniValue::VARR);
for (const CBlockIndex *pindex : headers) {
jsonHeaders.push_back(blockheaderToJSON(pindex));
{
LOCK(cs_main);
for (const CBlockIndex *pindex : headers) {
jsonHeaders.push_back(blockheaderToJSON(pindex));
}
}
std::string strJSON = jsonHeaders.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
Expand Down Expand Up @@ -239,7 +242,11 @@ static bool rest_block(HTTPRequest* req,
}

case RF_JSON: {
UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
UniValue objBlock;
{
LOCK(cs_main);
objBlock = blockToJSON(block, pblockindex, showTxDetails);
}
std::string strJSON = objBlock.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON);
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/blockchain.cpp
Expand Up @@ -78,6 +78,7 @@ double GetDifficulty(const CBlockIndex* blockindex)

UniValue blockheaderToJSON(const CBlockIndex* blockindex)
{
AssertLockHeld(cs_main);
UniValue result(UniValue::VOBJ);
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
int confirmations = -1;
Expand Down Expand Up @@ -106,6 +107,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)

UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails)
{
AssertLockHeld(cs_main);
UniValue result(UniValue::VOBJ);
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
int confirmations = -1;
Expand Down

0 comments on commit a9b6ba0

Please sign in to comment.