partial merge #13815: Add [[nodiscard]] to all {Decode,Parse}[...](...) functions returning bool (#4035)

* Merge #13815: Explicitly ignore the return value of DecodeBase58(...)

579497e77a

* Merge #13815: Default to DEFAULT_BLOCK_MIN_TX_FEE if unable to parse arg

7c5bc2a523

* Merge #13815: Add NODISCARD to all {Decode,Parse}[...](...) functions returning bool.

9cc0230cfc
This commit is contained in:
Kittywhiskers Van Gogh 2021-03-22 22:40:27 +05:30 committed by GitHub
parent bca9577b8f
commit e20dc9f374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 25 deletions

View File

@ -117,6 +117,7 @@ BITCOIN_CORE_H = \
addressindex.h \ addressindex.h \
spentindex.h \ spentindex.h \
addrman.h \ addrman.h \
attributes.h \
base58.h \ base58.h \
batchedlogger.h \ batchedlogger.h \
bech32.h \ bech32.h \

22
src/attributes.h Normal file
View File

@ -0,0 +1,22 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_ATTRIBUTES_H
#define BITCOIN_ATTRIBUTES_H
#if defined(__has_cpp_attribute)
# if __has_cpp_attribute(nodiscard)
# define NODISCARD [[nodiscard]]
# endif
#endif
#ifndef NODISCARD
# if defined(_MSC_VER) && _MSC_VER >= 1700
# define NODISCARD _Check_return_
# else
# define NODISCARD __attribute__((warn_unused_result))
# endif
#endif
#endif // BITCOIN_ATTRIBUTES_H

View File

@ -14,6 +14,8 @@
#ifndef BITCOIN_BASE58_H #ifndef BITCOIN_BASE58_H
#define BITCOIN_BASE58_H #define BITCOIN_BASE58_H
#include <attributes.h>
#include <string> #include <string>
#include <vector> #include <vector>
@ -33,13 +35,13 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch);
* return true if decoding is successful. * return true if decoding is successful.
* psz cannot be nullptr. * psz cannot be nullptr.
*/ */
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet); NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
/** /**
* Decode a base58-encoded string (str) into a byte vector (vchRet). * Decode a base58-encoded string (str) into a byte vector (vchRet).
* return true if decoding is successful. * return true if decoding is successful.
*/ */
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet); NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
/** /**
* Encode a byte vector into a base58-encoded string, including checksum * Encode a byte vector into a base58-encoded string, including checksum
@ -50,12 +52,12 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
* Decode a base58-encoded string (psz) that includes a checksum into a byte * Decode a base58-encoded string (psz) that includes a checksum into a byte
* vector (vchRet), return true if decoding is successful * vector (vchRet), return true if decoding is successful
*/ */
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet); NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
/** /**
* Decode a base58-encoded string (str) that includes a checksum into a byte * Decode a base58-encoded string (str) that includes a checksum into a byte
* vector (vchRet), return true if decoding is successful * vector (vchRet), return true if decoding is successful
*/ */
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet); NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
#endif // BITCOIN_BASE58_H #endif // BITCOIN_BASE58_H

View File

@ -49,7 +49,7 @@ static void Base58Decode(benchmark::State& state)
const char* addr = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem"; const char* addr = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem";
std::vector<unsigned char> vch; std::vector<unsigned char> vch;
while (state.KeepRunning()) { while (state.KeepRunning()) {
DecodeBase58(addr, vch); (void) DecodeBase58(addr, vch);
} }
} }

View File

@ -6,6 +6,7 @@
#define BITCOIN_CORE_IO_H #define BITCOIN_CORE_IO_H
#include <amount.h> #include <amount.h>
#include <attributes.h>
#include <string> #include <string>
#include <vector> #include <vector>
@ -22,8 +23,8 @@ struct CSpentIndexTxInfo;
// core_read.cpp // core_read.cpp
CScript ParseScript(const std::string& s); CScript ParseScript(const std::string& s);
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false); std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false);
bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx); NODISCARD bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx);
bool DecodeHexBlk(CBlock&, const std::string& strHexBlk); NODISCARD bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
uint256 ParseHashStr(const std::string&, const std::string& strName); uint256 ParseHashStr(const std::string&, const std::string& strName);
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName); std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);

View File

@ -83,9 +83,8 @@ static BlockAssembler::Options DefaultOptions(const CChainParams& params)
if (gArgs.IsArgSet("-blockmaxsize")) { if (gArgs.IsArgSet("-blockmaxsize")) {
options.nBlockMaxSize = gArgs.GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE); options.nBlockMaxSize = gArgs.GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
} }
if (gArgs.IsArgSet("-blockmintxfee")) { CAmount n = 0;
CAmount n = 0; if (gArgs.IsArgSet("-blockmintxfee") && ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n);
options.blockMinFeeRate = CFeeRate(n); options.blockMinFeeRate = CFeeRate(n);
} else { } else {
options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE); options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);

View File

@ -3,19 +3,20 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <attributes.h>
#include <chain.h> #include <chain.h>
#include <chainparams.h> #include <chainparams.h>
#include <core_io.h> #include <core_io.h>
#include <httpserver.h>
#include <primitives/block.h> #include <primitives/block.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <validation.h>
#include <httpserver.h>
#include <rpc/blockchain.h> #include <rpc/blockchain.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <streams.h> #include <streams.h>
#include <sync.h> #include <sync.h>
#include <txmempool.h> #include <txmempool.h>
#include <utilstrencodings.h> #include <utilstrencodings.h>
#include <validation.h>
#include <version.h> #include <version.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View File

@ -15,6 +15,7 @@
#include <config/dash-config.h> #include <config/dash-config.h>
#endif #endif
#include <attributes.h>
#include <compat.h> #include <compat.h>
#include <fs.h> #include <fs.h>
#include <logging.h> #include <logging.h>

View File

@ -9,16 +9,17 @@
#ifndef BITCOIN_UTILMONEYSTR_H #ifndef BITCOIN_UTILMONEYSTR_H
#define BITCOIN_UTILMONEYSTR_H #define BITCOIN_UTILMONEYSTR_H
#include <stdint.h>
#include <string>
#include <amount.h> #include <amount.h>
#include <attributes.h>
#include <cstdint>
#include <string>
/* Do not use these functions to represent or parse monetary amounts to or from /* Do not use these functions to represent or parse monetary amounts to or from
* JSON but use AmountFromValue and ValueFromAmount for that. * JSON but use AmountFromValue and ValueFromAmount for that.
*/ */
std::string FormatMoney(const CAmount& n); std::string FormatMoney(const CAmount& n);
bool ParseMoney(const std::string& str, CAmount& nRet); NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet);
bool ParseMoney(const char* pszIn, CAmount& nRet); NODISCARD bool ParseMoney(const char* pszIn, CAmount& nRet);
#endif // BITCOIN_UTILMONEYSTR_H #endif // BITCOIN_UTILMONEYSTR_H

View File

@ -261,7 +261,7 @@ std::string DecodeBase32(const std::string& str)
return std::string((const char*)vchRet.data(), vchRet.size()); return std::string((const char*)vchRet.data(), vchRet.size());
} }
static bool ParsePrechecks(const std::string& str) NODISCARD static bool ParsePrechecks(const std::string& str)
{ {
if (str.empty()) // No empty string allowed if (str.empty()) // No empty string allowed
return false; return false;

View File

@ -9,7 +9,9 @@
#ifndef BITCOIN_UTILSTRENCODINGS_H #ifndef BITCOIN_UTILSTRENCODINGS_H
#define BITCOIN_UTILSTRENCODINGS_H #define BITCOIN_UTILSTRENCODINGS_H
#include <stdint.h> #include <attributes.h>
#include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
@ -76,35 +78,35 @@ constexpr bool IsDigit(char c)
* @returns true if the entire string could be parsed as valid integer, * @returns true if the entire string could be parsed as valid integer,
* false if not the entire string could be parsed or when overflow or underflow occurred. * false if not the entire string could be parsed or when overflow or underflow occurred.
*/ */
bool ParseInt32(const std::string& str, int32_t *out); NODISCARD bool ParseInt32(const std::string& str, int32_t *out);
/** /**
* Convert string to signed 64-bit integer with strict parse error feedback. * Convert string to signed 64-bit integer with strict parse error feedback.
* @returns true if the entire string could be parsed as valid integer, * @returns true if the entire string could be parsed as valid integer,
* false if not the entire string could be parsed or when overflow or underflow occurred. * false if not the entire string could be parsed or when overflow or underflow occurred.
*/ */
bool ParseInt64(const std::string& str, int64_t *out); NODISCARD bool ParseInt64(const std::string& str, int64_t *out);
/** /**
* Convert decimal string to unsigned 32-bit integer with strict parse error feedback. * Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
* @returns true if the entire string could be parsed as valid integer, * @returns true if the entire string could be parsed as valid integer,
* false if not the entire string could be parsed or when overflow or underflow occurred. * false if not the entire string could be parsed or when overflow or underflow occurred.
*/ */
bool ParseUInt32(const std::string& str, uint32_t *out); NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out);
/** /**
* Convert decimal string to unsigned 64-bit integer with strict parse error feedback. * Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
* @returns true if the entire string could be parsed as valid integer, * @returns true if the entire string could be parsed as valid integer,
* false if not the entire string could be parsed or when overflow or underflow occurred. * false if not the entire string could be parsed or when overflow or underflow occurred.
*/ */
bool ParseUInt64(const std::string& str, uint64_t *out); NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
/** /**
* Convert string to double with strict parse error feedback. * Convert string to double with strict parse error feedback.
* @returns true if the entire string could be parsed as valid double, * @returns true if the entire string could be parsed as valid double,
* false if not the entire string could be parsed or when overflow or underflow occurred. * false if not the entire string could be parsed or when overflow or underflow occurred.
*/ */
bool ParseDouble(const std::string& str, double *out); NODISCARD bool ParseDouble(const std::string& str, double *out);
template<typename T> template<typename T>
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false) std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
@ -157,7 +159,7 @@ bool TimingResistantEqual(const T& a, const T& b)
* @returns true on success, false on error. * @returns true on success, false on error.
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger. * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
*/ */
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
/** Convert from one power-of-2 number base to another. */ /** Convert from one power-of-2 number base to another. */
template<int frombits, int tobits, bool pad, typename O, typename I> template<int frombits, int tobits, bool pad, typename O, typename I>