Skip to content

Conversation

jonatack
Copy link
Member

Seen while compiling with clang:

  CXX      libbitcoin_server_a-validationinterface.o
txmempool.cpp: In member function ‘CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<uint256>&)’:
txmempool.cpp:883:29: warning: ‘<anonymous>’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  883 |     return Optional<txiter>{};
clang --version
clang version 9.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@@ -880,7 +880,7 @@ Optional<CTxMemPool::txiter> CTxMemPool::GetIter(const uint256& txid) const
{
auto it = mapTx.find(txid);
if (it != mapTx.end()) return it;
return Optional<txiter>{};
return Optional<txiter>{nullptr};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 faa1a74 (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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be return nullopt or return Optional<txiter>{nullopt} I think. I'd assume nullptr gets coerced to a txiter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jonatack
Copy link
Member Author

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.

@jonatack
Copy link
Member Author

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.

@jonatack jonatack closed this Dec 29, 2020
@jonatack jonatack deleted the fix-txmempool-maybe-uninitialized-txiter branch December 29, 2020 19:15
@bitcoin bitcoin locked and limited conversation to collaborators Jan 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants