Skip to content

Commit cb77dc8

Browse files
committed
Merge #15292: Remove 'boost::optional'-related false positive -Wmaybe-uninitialized warnings on GCC compiler
2d48314 Remove 'boost::optional'-related gcc warnings (Hennadii Stepanov) Pull request description: #14711 introduced some warnings when building with gcc compiler. See: - bitcoin/bitcoin#14711 (comment) by @laanwj - bitcoin/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/bitcoin#15292 (comment)) by @Empact) This PR removes these warnings. cc: @Empact @practicalswift Tree-SHA512: 752ae3c3ca6282bbf98726236fbc3069ab9d1aee57ae2ec2668b32e4541e7bc1acb15b7d6fa9e2b6daf1ec29c0987a1053ee1ca0f523b71367ff911221c58c94
2 parents bb36f5e + 2d48314 commit cb77dc8

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/optional.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@
55
#ifndef BITCOIN_OPTIONAL_H
66
#define BITCOIN_OPTIONAL_H
77

8+
#include <utility>
9+
810
#include <boost/optional.hpp>
911

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

16+
//! Substitute for C++17 std::make_optional
17+
template <typename T>
18+
Optional<T> MakeOptional(bool condition, T&& value)
19+
{
20+
return boost::make_optional(condition, std::forward<T>(value));
21+
}
22+
1423
//! Substitute for C++17 std::nullopt
1524
static auto& nullopt = boost::none;
1625

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,8 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
15911591
auto locked_chain = pwallet->chain().lock();
15921592
LOCK(pwallet->cs_wallet);
15931593

1594-
Optional<int> height; // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
1594+
// The way the 'height' is initialized is just a workaround for the gcc bug #47679 since version 4.6.0.
1595+
Optional<int> height = MakeOptional(false, int()); // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
15951596
Optional<int> altheight; // Height of the specified block, even if it's in a deactivated chain.
15961597
int target_confirms = 1;
15971598
isminefilter filter = ISMINE_SPENDABLE;

src/wallet/wallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,8 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
16421642
fAbortRescan = false;
16431643
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
16441644
uint256 tip_hash;
1645-
Optional<int> block_height;
1645+
// The way the 'block_height' is initialized is just a workaround for the gcc bug #47679 since version 4.6.0.
1646+
Optional<int> block_height = MakeOptional(false, int());
16461647
double progress_begin;
16471648
double progress_end;
16481649
{

0 commit comments

Comments
 (0)