From 2168ba9d2e6c9666e98d0b0a079399b4fa3e332c Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 6 Sep 2017 23:53:55 +0200 Subject: [PATCH] Merge #11238: Add assertions before potential null deferences c00199244 Fix potential null dereferences (MeshCollider) Pull request description: Picked up by the static analyzer [Facebook Infer](http://fbinfer.com/) which I was playing around with for another research project. Just adding some asserts before dereferencing potentially null pointers. Tree-SHA512: 9c01dab2d21bce75c7c7ef867236654ab538318a1fb39f96f09cdd2382a05be1a6b2db0a1169a94168864e82ffeae0686a383db6eba799742bdd89c37ac74397 --- src/miner.cpp | 1 + src/net_processing.cpp | 1 + src/rpc/blockchain.cpp | 3 +++ src/validation.cpp | 2 ++ 4 files changed, 7 insertions(+) diff --git a/src/miner.cpp b/src/miner.cpp index 68aa079b33f593..96060ccd6c3747 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -136,6 +136,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc LOCK2(cs_main, mempool.cs); CBlockIndex* pindexPrev = chainActive.Tip(); + assert(pindexPrev != nullptr); nHeight = pindexPrev->nHeight + 1; bool fDIP0003Active_context = nHeight >= chainparams.GetConsensus().DIP0003Height; diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 548d42626114ca..1acdb3777eb1ff 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -337,6 +337,7 @@ bool MarkBlockAsReceived(const uint256& hash) { std::map::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash); if (itInFlight != mapBlocksInFlight.end()) { CNodeState *state = State(itInFlight->second.first); + assert(state != nullptr); state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders; if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) { // Last validated block on the queue was received. diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 3503dfb4d25d1f..cd2f2269c7dd38 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1080,6 +1080,7 @@ static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash, static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats) { std::unique_ptr pcursor(view->Cursor()); + assert(pcursor); CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); stats.hashBlock = pcursor->GetBestBlock(); @@ -1815,6 +1816,8 @@ UniValue getchaintxstats(const JSONRPCRequest& request) pindex = chainActive.Tip(); } } + + assert(pindex != nullptr); if (blockcount < 1 || blockcount >= pindex->nHeight) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block count: should be between 1 and the block's height"); diff --git a/src/validation.cpp b/src/validation.cpp index 76dd35db4d5984..461734d00956f2 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -290,6 +290,8 @@ bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool AssertLockHeld(mempool.cs); CBlockIndex* tip = chainActive.Tip(); + assert(tip != nullptr); + CBlockIndex index; index.pprev = tip; // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate