From 53a3fd13da081a79a8d7ec9842057924dec32957 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 | 2 ++ src/rpc/blockchain.cpp | 3 +++ src/validation.cpp | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/miner.cpp b/src/miner.cpp index f03a85ff06f14..7a643afe42cf1 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 75c77ced43b4b..06cd79d61b560 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -316,6 +316,7 @@ void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) { fUpdateConnectionTime = false; LOCK(cs_main); CNodeState *state = State(nodeid); + assert(state != nullptr); if (state->fSyncStarted) nSyncStarted--; @@ -350,6 +351,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 1b48e9cc9dac9..83e3a20e29883 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1079,6 +1079,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(); @@ -1812,6 +1813,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 803e8ae20adc7..74d48b16fbb91 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -270,6 +270,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