Skip to content

Commit 384967f

Browse files
author
MarcoFalke
committed
Merge #13815: util: Add [[nodiscard]] to all {Decode,Parse}[...](...) functions returning bool
9cc0230 Add NODISCARD to all {Decode,Parse}[...](...) functions returning bool. Sort includes. (practicalswift) 579497e tests: Explicitly ignore the return value of DecodeBase58(...) (practicalswift) 145fe95 tests: Check return value of ParseParameters(...) (practicalswift) 7c5bc2a miner: Default to DEFAULT_BLOCK_MIN_TX_FEE if unable to parse -blockmintxfee (practicalswift) Pull request description: Changes in this PR: * ~~Add linter to make sure the return value of `Parse[...](...)` is checked~~ * Add `__attribute__((warn_unused_result))` to all `{Decode,Parse}[...](...)` functions returning `bool` * Fix violations Context: * #13712: `wallet: Fix non-determinism in ParseHDKeypath(...). Avoid using an uninitialized variable in path calculation.` would have been prevented by this Tree-SHA512: 41a97899f2d5a26584235fa02b1ebfb4faacd81ea97e927022955a658fa7e15d07a1443b4b7635151a43259a1adf8f2f4de3c1c75d7b5f09f0d5496463a1dae6
2 parents e74649e + 9cc0230 commit 384967f

File tree

15 files changed

+84
-53
lines changed

15 files changed

+84
-53
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ endif
9595
BITCOIN_CORE_H = \
9696
addrdb.h \
9797
addrman.h \
98+
attributes.h \
9899
base58.h \
99100
bech32.h \
100101
bloom.h \

src/attributes.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2018 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_ATTRIBUTES_H
7+
#define BITCOIN_ATTRIBUTES_H
8+
9+
#if defined(__has_cpp_attribute)
10+
# if __has_cpp_attribute(nodiscard)
11+
# define NODISCARD [[nodiscard]]
12+
# endif
13+
#endif
14+
#ifndef NODISCARD
15+
# if defined(_MSC_VER) && _MSC_VER >= 1700
16+
# define NODISCARD _Check_return_
17+
# else
18+
# define NODISCARD __attribute__((warn_unused_result))
19+
# endif
20+
#endif
21+
22+
#endif // BITCOIN_ATTRIBUTES_H

src/base58.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef BITCOIN_BASE58_H
1515
#define BITCOIN_BASE58_H
1616

17+
#include <attributes.h>
18+
1719
#include <string>
1820
#include <vector>
1921

@@ -33,13 +35,13 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch);
3335
* return true if decoding is successful.
3436
* psz cannot be nullptr.
3537
*/
36-
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
38+
NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
3739

3840
/**
3941
* Decode a base58-encoded string (str) into a byte vector (vchRet).
4042
* return true if decoding is successful.
4143
*/
42-
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
44+
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
4345

4446
/**
4547
* Encode a byte vector into a base58-encoded string, including checksum
@@ -50,12 +52,12 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
5052
* Decode a base58-encoded string (psz) that includes a checksum into a byte
5153
* vector (vchRet), return true if decoding is successful
5254
*/
53-
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
55+
NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
5456

5557
/**
5658
* Decode a base58-encoded string (str) that includes a checksum into a byte
5759
* vector (vchRet), return true if decoding is successful
5860
*/
59-
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
61+
NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
6062

6163
#endif // BITCOIN_BASE58_H

src/bench/base58.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void Base58Decode(benchmark::State& state)
4949
const char* addr = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem";
5050
std::vector<unsigned char> vch;
5151
while (state.KeepRunning()) {
52-
DecodeBase58(addr, vch);
52+
(void) DecodeBase58(addr, vch);
5353
}
5454
}
5555

src/core_io.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_CORE_IO_H
77

88
#include <amount.h>
9+
#include <attributes.h>
910

1011
#include <string>
1112
#include <vector>
@@ -22,8 +23,8 @@ class UniValue;
2223
// core_read.cpp
2324
CScript ParseScript(const std::string& s);
2425
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false);
25-
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
26-
bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
26+
NODISCARD bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
27+
NODISCARD bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
2728
bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
2829

2930
/**
@@ -36,7 +37,7 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
3637
*/
3738
bool ParseHashStr(const std::string& strHex, uint256& result);
3839
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
39-
bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error);
40+
NODISCARD bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error);
4041
int ParseSighashString(const UniValue& sighash);
4142

4243
// core_write.cpp

src/miner.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ static BlockAssembler::Options DefaultOptions()
7070
// If -blockmaxweight is not given, limit to DEFAULT_BLOCK_MAX_WEIGHT
7171
BlockAssembler::Options options;
7272
options.nBlockMaxWeight = gArgs.GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
73-
if (gArgs.IsArgSet("-blockmintxfee")) {
74-
CAmount n = 0;
75-
ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n);
73+
CAmount n = 0;
74+
if (gArgs.IsArgSet("-blockmintxfee") && ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
7675
options.blockMinFeeRate = CFeeRate(n);
7776
} else {
7877
options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);

src/outputtype.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_OUTPUTTYPE_H
77
#define BITCOIN_OUTPUTTYPE_H
88

9+
#include <attributes.h>
910
#include <keystore.h>
1011
#include <script/standard.h>
1112

@@ -26,7 +27,7 @@ enum class OutputType {
2627
CHANGE_AUTO,
2728
};
2829

29-
bool ParseOutputType(const std::string& str, OutputType& output_type);
30+
NODISCARD bool ParseOutputType(const std::string& str, OutputType& output_type);
3031
const std::string& FormatOutputType(OutputType type);
3132

3233
/**

src/rest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#include <attributes.h>
67
#include <chain.h>
78
#include <chainparams.h>
89
#include <core_io.h>
10+
#include <httpserver.h>
911
#include <index/txindex.h>
1012
#include <primitives/block.h>
1113
#include <primitives/transaction.h>
12-
#include <validation.h>
13-
#include <httpserver.h>
1414
#include <rpc/blockchain.h>
1515
#include <rpc/server.h>
1616
#include <streams.h>
1717
#include <sync.h>
1818
#include <txmempool.h>
1919
#include <util/strencodings.h>
20+
#include <validation.h>
2021
#include <version.h>
2122

2223
#include <boost/algorithm/string.hpp>

src/script/descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ std::vector<Span<const char>> Split(const Span<const char>& sp, char sep)
478478
}
479479

480480
/** Parse a key path, being passed a split list of elements (the first element is ignored). */
481-
bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out)
481+
NODISCARD bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out)
482482
{
483483
for (size_t i = 1; i < split.size(); ++i) {
484484
Span<const char> elem = split[i];

src/test/getarg_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void ResetArgs(const std::string& strArg)
2828
vecChar.push_back(s.c_str());
2929

3030
std::string error;
31-
gArgs.ParseParameters(vecChar.size(), vecChar.data(), error);
31+
BOOST_CHECK(gArgs.ParseParameters(vecChar.size(), vecChar.data(), error));
3232
}
3333

3434
static void SetupArgs(const std::vector<std::string>& args)

0 commit comments

Comments
 (0)