consensus: fix maybe uninitialized CTxMemPool::GetIter() #20797

pull jonatack wants to merge 1 commits into bitcoin:master from jonatack:fix-txmempool-maybe-uninitialized-txiter changing 1 files +1 −1
  1. jonatack commented at 12:57 pm on December 29, 2020: member

    Seen while compiling with clang:

    0  CXX      libbitcoin_server_a-validationinterface.o
    1txmempool.cpp: In member function CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<uint256>&):
    2txmempool.cpp:883:29: warning: <anonymous> may be used uninitialized in this function [-Wmaybe-uninitialized]
    3  883 |     return Optional<txiter>{};
    
    0clang --version
    1clang version 9.0.1
    2Target: x86_64-pc-linux-gnu
    3Thread model: posix
    4InstalledDir: /usr/bin
    
  2. consensus: fix maybe uninitialized CTxMemPool::GetIter() aeafdd56bf
  3. in src/txmempool.cpp:883 in aeafdd56bf
    879@@ -880,7 +880,7 @@ Optional<CTxMemPool::txiter> CTxMemPool::GetIter(const uint256& txid) const
    880 {
    881     auto it = mapTx.find(txid);
    882     if (it != mapTx.end()) return it;
    883-    return Optional<txiter>{};
    884+    return Optional<txiter>{nullptr};
    


    MarcoFalke commented at 1:09 pm on December 29, 2020:

    Mind to explain how this fix works and why it is correct. I might be missing something, but https://en.cppreference.com/w/cpp/utility/optional/optional doesn’t list a constructor that accepts a nullptr.

    Unrelated, this method was added in faa1a749428a195af784633eb78e1df5d6a0e875 (by me) to not expose mapTx, which is an implementation detail. Though, I realize that GetIter doesn’t follow the iterator-scheme (find(), end(), begin(), …). It may be better to hide the mapTx from outside by exposing Find(), End(), Begin(), which are internally forwarded to the corresponding mapTx methods.


    ajtowns commented at 1:17 pm on December 29, 2020:
    This should be return nullopt or return Optional<txiter>{nullopt} I think. I’d assume nullptr gets coerced to a txiter?

    vasild commented at 3:06 pm on December 29, 2020:

    I don’t get it why there is a warning in the first place. std::optional<txiter>{} could call the optional’s constructor that “Constructs an object that does not contain a value”, same as std::optional<txiter>{std::nullopt}. Would std::optional<txiter>() fix it?

    Clang has no -Wmaybe-uninitialized option, it must be gcc that prints this (OP mentions clang)?

    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635 may be related.


    MarcoFalke commented at 3:21 pm on December 29, 2020:

    I don’t get it why there is a warning in the first place.

    GCC’s warnings are very brittle, which can be seen from the open issues in their bug tracker. Personally I’ve seen many gcc warnings appear/disappear depending on optimization level or trivial code restructuring. It would be a full time job to report all these inconsistencies on the bugtracker.

  4. DrahtBot added the label Consensus on Dec 29, 2020
  5. DrahtBot added the label Mempool on Dec 29, 2020
  6. MarcoFalke removed the label Consensus on Dec 29, 2020
  7. MarcoFalke added the label Refactoring on Dec 29, 2020
  8. jonatack commented at 2:42 pm on December 29, 2020: member
    Thanks for the feedback. I had to be away from the computer for a couple hours but will look carefully at a better solution this afternoon.
  9. jonatack commented at 7:12 pm on December 29, 2020: member
    I saw this warning three times during the morning. On returning this afternoon, I could no longer reproduce, idem after rebooting, and after apt updating. Closing with apologies for the noise.
  10. jonatack closed this on Dec 29, 2020

  11. jonatack deleted the branch on Dec 29, 2020
  12. fanquake locked this on Jan 3, 2021

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: 2024-07-06 01:12 UTC

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