From be42c0d80e1de2bfd4f2658014eda18cab5a398d Mon Sep 17 00:00:00 2001 From: Pavlos Antoniou Date: Tue, 20 Jun 2017 05:32:34 +0000 Subject: [PATCH] [rpc] Avoid possibility of NULL pointer dereference in getblockchaininfo(...) The variable *block is initialized by the return value of Tip() which may be NULL. The while loop condition takes this into account and checks for nullness before dereferencing. If Tip() returns null, the while loop is never executed and the null pointer is dereferenced right after. An assertion is added before dereferencing the return value if Tip(). --- src/rpc/blockchain.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 8f7f76841d3..baf8daee51a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1165,6 +1165,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) + HelpExampleRpc("getblockchaininfo", "") ); + assert(chainActive.Tip() && "An empty blockchain should not be possible here."); LOCK(cs_main); UniValue obj(UniValue::VOBJ); @@ -1196,6 +1197,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) block = block->pprev; + assert(block && "An empty blockchain should not be possible here."); obj.push_back(Pair("pruneheight", block->nHeight)); } return obj;