Skip to content

Commit

Permalink
Fix potential null dereferences
Browse files Browse the repository at this point in the history
  • Loading branch information
meshcollider authored and random-zebra committed Jul 25, 2021
1 parent 80f35f9 commit 732bb9d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/net_processing.cpp
Expand Up @@ -232,7 +232,7 @@ CNodeState* State(NodeId pnode)
{
std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
if (it == mapNodeState.end())
return NULL;
return nullptr;
return &it->second;
}

Expand Down Expand Up @@ -272,6 +272,7 @@ void MarkBlockAsReceived(const uint256& hash)
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
if (itInFlight != mapBlocksInFlight.end()) {
CNodeState* state = State(itInFlight->second.first);
assert(state != nullptr);
nQueuedValidatedHeaders -= itInFlight->second.second->fValidatedHeaders;
state->vBlocksInFlight.erase(itInFlight->second.second);
state->nBlocksInFlight--;
Expand All @@ -284,7 +285,7 @@ void MarkBlockAsReceived(const uint256& hash)
void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr)
{
CNodeState* state = State(nodeid);
assert(state != NULL);
assert(state != nullptr);

// Make sure it's not listed somewhere already.
MarkBlockAsReceived(hash);
Expand All @@ -300,7 +301,7 @@ void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex*
void ProcessBlockAvailability(NodeId nodeid)
{
CNodeState* state = State(nodeid);
assert(state != NULL);
assert(state != nullptr);

if (!state->hashLastUnknownBlock.IsNull()) {
BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
Expand All @@ -316,7 +317,7 @@ void ProcessBlockAvailability(NodeId nodeid)
void UpdateBlockAvailability(NodeId nodeid, const uint256& hash)
{
CNodeState* state = State(nodeid);
assert(state != NULL);
assert(state != nullptr);

ProcessBlockAvailability(nodeid);

Expand All @@ -340,7 +341,7 @@ void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<con

vBlocks.reserve(vBlocks.size() + count);
CNodeState* state = State(nodeid);
assert(state != NULL);
assert(state != nullptr);

// Make sure pindexBestKnownBlock is up to date, we'll need it.
ProcessBlockAvailability(nodeid);
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/blockchain.cpp
Expand Up @@ -727,6 +727,7 @@ static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash,
static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
{
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
assert(pcursor);

CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
stats.hashBlock = pcursor->GetBestBlock();
Expand Down Expand Up @@ -864,6 +865,7 @@ UniValue gettxout(const JSONRPCRequest& request)

BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
CBlockIndex* pindex = it->second;
assert(pindex != nullptr);
ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
if (coin.nHeight == MEMPOOL_HEIGHT) {
ret.pushKV("confirmations", 0);
Expand Down
1 change: 1 addition & 0 deletions src/validation.cpp
Expand Up @@ -1870,6 +1870,7 @@ void static UpdateTip(CBlockIndex* pindexNew)
}

const CBlockIndex* pChainTip = chainActive.Tip();
assert(pChainTip != nullptr);
LogPrintf("%s: new best=%s height=%d version=%d log2_work=%.16f tx=%lu date=%s progress=%f cache=%.1fMiB(%utxo) evodb_cache=%.1fMiB\n",
__func__,
pChainTip->GetBlockHash().GetHex(), pChainTip->nHeight, pChainTip->nVersion, log(pChainTip->nChainWork.getdouble()) / log(2.0), (unsigned long)pChainTip->nChainTx,
Expand Down

0 comments on commit 732bb9d

Please sign in to comment.