Skip to content

Commit

Permalink
Merge bitcoin#15292: Remove 'boost::optional'-related false positive …
Browse files Browse the repository at this point in the history
…-Wmaybe-uninitialized warnings on GCC compiler (dashpay#4635)

2d48314 Remove 'boost::optional'-related gcc warnings (Hennadii Stepanov)

Pull request description:

  bitcoin#14711 introduced some warnings when building with gcc compiler.

  See:
  - bitcoin#14711 (comment) by @laanwj
  - bitcoin#14711 (review) by @ryanofsky

  This gcc [issue](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47679) has been known since version 4.6.0 and last updated in 2017.
  From the boost [docs](https://www.boost.org/doc/libs/1_69_0/libs/optional/doc/html/boost_optional/quick_start/optional_automatic_variables.html):
  > The default constructor of `optional` creates an _uninitialized_ `optional` object.

  Also: [False positive with -Wmaybe-uninitialized](https://www.boost.org/doc/libs/1_69_0/libs/optional/doc/html/boost_optional/tutorial/gotchas/false_positive_with__wmaybe_uninitialized.html) ([pointed out](bitcoin#15292 (comment)) by @Empact)

  This PR removes these warnings.

  cc: @Empact @practicalswift

Tree-SHA512: 752ae3c3ca6282bbf98726236fbc3069ab9d1aee57ae2ec2668b32e4541e7bc1acb15b7d6fa9e2b6daf1ec29c0987a1053ee1ca0f523b71367ff911221c58c94

Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
  • Loading branch information
PastaPastaPasta and laanwj authored Dec 22, 2021
1 parent 5bc79fe commit 0645d47
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
#ifndef BITCOIN_OPTIONAL_H
#define BITCOIN_OPTIONAL_H

#include <utility>

#include <boost/optional.hpp>

//! Substitute for C++17 std::optional
template <typename T>
using Optional = boost::optional<T>;

//! Substitute for C++17 std::make_optional
template <typename T>
Optional<T> MakeOptional(bool condition, T&& value)
{
return boost::make_optional(condition, std::forward<T>(value));
}

//! Substitute for C++17 std::nullopt
static auto& nullopt = boost::none;

Expand Down
3 changes: 2 additions & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,8 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
LockAnnotation lock(::cs_main);
LOCK(pwallet->cs_wallet);

Optional<int> height; // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
// The way the 'height' is initialized is just a workaround for the gcc bug #47679 since version 4.6.0.
Optional<int> height = MakeOptional(false, int()); // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
Optional<int> altheight; // Height of the specified block, even if it's in a deactivated chain.
int target_confirms = 1;
isminefilter filter = ISMINE_SPENDABLE;
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,8 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
fAbortRescan = false;
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
uint256 tip_hash;
Optional<int> block_height;
// The way the 'block_height' is initialized is just a workaround for the gcc bug #47679 since version 4.6.0.
Optional<int> block_height = MakeOptional(false, int());
double progress_begin;
double progress_end;
{
Expand Down

0 comments on commit 0645d47

Please sign in to comment.