[rpc] Avoid possibility of NULL pointer dereference in getblocktemplate(...) #9560

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:avoid-null-pointer-dereference-in-rpc-blockchain changing 1 files +1 −0
  1. practicalswift commented at 11:03 PM on January 14, 2017: contributor

    Prior to this commit there was an implicit assumption that the following code path would always be taken ...

    if (pindexPrev != chainActive.Tip() ||
        (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))

    ... otherwise pblock would end up being NULL, and given ...

    pblock->nNonce = 0;

    ... we would have a NULL pointer dereference.

    This commit adds an assertion which removes the possibility of a NULL pointer dereference.

  2. [rpc] Avoid possibility of NULL pointer dereference in getblocktemplate(...)
    Prior to this commit there was an implicit assumption that the following
    code path would always be taken ...
    
        if (pindexPrev != chainActive.Tip() ||
            (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
    
    ... otherwise pblock would end up being NULL, and given ...
    
        pblock->nNonce = 0;
    
    ... we would have a NULL pointer dereference.
    
    This commit adds an assertion which removes the possibility of a NULL
    pointer dereference.
    796502f33f
  3. in src/rpc/mining.cpp:None in 796502f33f
     543 | @@ -544,6 +544,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
     544 |          pindexPrev = pindexPrevNew;
     545 |      }
     546 |      CBlock* pblock = &pblocktemplate->block; // pointer for convenience
     547 | +    assert(pblock);
    


    luke-jr commented at 11:55 PM on January 14, 2017:

    It's impossible for pblock to be NULL here. The dereference is one line above. And pindexPrev initialises to NULL, so the above initialisation will indeed always occur.


    practicalswift commented at 12:16 AM on January 15, 2017:

    The dereference I'm trying to avoid is not on the line above, but a couple of lines below - see here (pblock->nNonce = 0;).

    Is your reading that if-branch on lines 526-546 will always be taken?

    If that branch is not taken then pblock would be NULL, right? :-)


    practicalswift commented at 12:32 AM on January 15, 2017:

    Sorry. After re-reading the comment and the code I think I understand - your reading is that the if-branch is guaranteed to be taken on the first call, right? :-)


    practicalswift commented at 12:49 AM on January 15, 2017:

    I've now re-read the code and now my reading is that assuming the if-branch is taken on the first call then there is no possibility of a NULL pointer dereference.

    The question then boils down to if pindexPrev != chainActive.Tip() || (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5) is guaranteed to be true on the first call :-)


    luke-jr commented at 2:41 AM on January 15, 2017:

    It is guaranteed to be true on the first call.

    But even if it wasn't, pblock would still never be NULL. It is always assigned to &pblocktemplate->block, which might be NULL with some platforms, but is not guaranteed to be, and if not would pass your assert yet still be wrong.


    practicalswift commented at 2:51 AM on January 15, 2017:

    OK, thanks for reviewing. Closing PR.

  4. practicalswift closed this on Jan 15, 2017

  5. practicalswift deleted the branch on Apr 10, 2021
  6. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-16 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me