Skip to content

Commit 9ccaee1

Browse files
author
MarcoFalke
committed
Merge #19004: refactor: Replace const char* to std::string
c57f03c refactor: Replace const char* to std::string (Calvin Kim) Pull request description: Rationale: Addresses #19000 Some functions should be returning std::string instead of const char*. This commit changes that. Main benefits/reasoning: 1. The functions never return nullptr, so returning a string makes code at call sites easier to review (reviewers don't have to read the source code to verify that a nullptr is never returned) 2. All call sites convert to string anyway ACKs for top commit: MarcoFalke: re-ACK c57f03c (no changes since previous review) 🚃 Empact: Fair enough, Code Review ACK bitcoin/bitcoin@c57f03c practicalswift: ACK c57f03c -- patch looks correct hebasto: re-ACK c57f03c Tree-SHA512: 9ce99bb38fe399b54844315048204cafce0f27fd8f24cae357fa7ac6f5d8094d57bbf5f5c1f5878a65f2d35e4a3f95d527eb17f49250b690c591c0df86ca84fd
2 parents cffbf1e + c57f03c commit 9ccaee1

File tree

9 files changed

+24
-13
lines changed

9 files changed

+24
-13
lines changed

src/bitcoin-cli.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <functional>
2222
#include <memory>
2323
#include <stdio.h>
24+
#include <string>
2425
#include <tuple>
2526

2627
#include <event2/buffer.h>
@@ -158,7 +159,7 @@ struct HTTPReply
158159
std::string body;
159160
};
160161

161-
static const char *http_errorstring(int code)
162+
static std::string http_errorstring(int code)
162163
{
163164
switch(code) {
164165
#if LIBEVENT_VERSION_NUMBER >= 0x02010300

src/core_read.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <boost/algorithm/string/split.hpp>
2020

2121
#include <algorithm>
22+
#include <string>
2223

2324
CScript ParseScript(const std::string& s)
2425
{
@@ -34,10 +35,9 @@ CScript ParseScript(const std::string& s)
3435
if (op < OP_NOP && op != OP_RESERVED)
3536
continue;
3637

37-
const char* name = GetOpName(static_cast<opcodetype>(op));
38-
if (strcmp(name, "OP_UNKNOWN") == 0)
38+
std::string strName = GetOpName(static_cast<opcodetype>(op));
39+
if (strName == "OP_UNKNOWN")
3940
continue;
40-
std::string strName(name);
4141
mapOpNames[strName] = static_cast<opcodetype>(op);
4242
// Convenience: OP_ADD and just ADD are both recognized:
4343
boost::algorithm::replace_first(strName, "OP_", "");

src/script/script.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include <util/strencodings.h>
99

10-
const char* GetOpName(opcodetype opcode)
10+
#include <string>
11+
12+
std::string GetOpName(opcodetype opcode)
1113
{
1214
switch (opcode)
1315
{

src/script/script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ enum opcodetype
193193
// Maximum value that an opcode can be
194194
static const unsigned int MAX_OPCODE = OP_NOP10;
195195

196-
const char* GetOpName(opcodetype opcode);
196+
std::string GetOpName(opcodetype opcode);
197197

198198
class scriptnum_error : public std::runtime_error
199199
{

src/script/script_error.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
#include <script/script_error.h>
77

8-
const char* ScriptErrorString(const ScriptError serror)
8+
#include <string>
9+
10+
std::string ScriptErrorString(const ScriptError serror)
911
{
1012
switch (serror)
1113
{

src/script/script_error.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef BITCOIN_SCRIPT_SCRIPT_ERROR_H
77
#define BITCOIN_SCRIPT_SCRIPT_ERROR_H
88

9+
#include <string>
10+
911
typedef enum ScriptError_t
1012
{
1113
SCRIPT_ERR_OK = 0,
@@ -73,6 +75,6 @@ typedef enum ScriptError_t
7375

7476
#define SCRIPT_ERR_LAST SCRIPT_ERR_ERROR_COUNT
7577

76-
const char* ScriptErrorString(const ScriptError error);
78+
std::string ScriptErrorString(const ScriptError error);
7779

7880
#endif // BITCOIN_SCRIPT_SCRIPT_ERROR_H

src/script/standard.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <pubkey.h>
1010
#include <script/script.h>
1111

12+
#include <string>
13+
1214
typedef std::vector<unsigned char> valtype;
1315

1416
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
@@ -25,7 +27,7 @@ WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
2527
CSHA256().Write(in.data(), in.size()).Finalize(begin());
2628
}
2729

28-
const char* GetTxnOutputType(txnouttype t)
30+
std::string GetTxnOutputType(txnouttype t)
2931
{
3032
switch (t)
3133
{
@@ -39,7 +41,7 @@ const char* GetTxnOutputType(txnouttype t)
3941
case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
4042
case TX_WITNESS_UNKNOWN: return "witness_unknown";
4143
}
42-
return nullptr;
44+
assert(false);
4345
}
4446

4547
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)

src/script/standard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <boost/variant.hpp>
1313

14+
#include <string>
15+
1416

1517
static const bool DEFAULT_ACCEPT_DATACARRIER = true;
1618

@@ -145,7 +147,7 @@ typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash,
145147
bool IsValidDestination(const CTxDestination& dest);
146148

147149
/** Get the name of a txnouttype as a C string, or nullptr if unknown. */
148-
const char* GetTxnOutputType(txnouttype t);
150+
std::string GetTxnOutputType(txnouttype t);
149151

150152
/**
151153
* Parse a scriptPubKey and identify script type for standard scripts. If

src/test/script_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static ScriptErrorDesc script_errors[]={
102102
{SCRIPT_ERR_SIG_FINDANDDELETE, "SIG_FINDANDDELETE"},
103103
};
104104

105-
static const char *FormatScriptError(ScriptError_t err)
105+
static std::string FormatScriptError(ScriptError_t err)
106106
{
107107
for (unsigned int i=0; i<ARRAYLEN(script_errors); ++i)
108108
if (script_errors[i].err == err)
@@ -134,7 +134,7 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
134134
CMutableTransaction tx = BuildSpendingTransaction(scriptSig, scriptWitness, txCredit);
135135
CMutableTransaction tx2 = tx;
136136
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue), &err) == expect, message);
137-
BOOST_CHECK_MESSAGE(err == scriptError, std::string(FormatScriptError(err)) + " where " + std::string(FormatScriptError((ScriptError_t)scriptError)) + " expected: " + message);
137+
BOOST_CHECK_MESSAGE(err == scriptError, FormatScriptError(err) + " where " + FormatScriptError((ScriptError_t)scriptError) + " expected: " + message);
138138

139139
// Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
140140
for (int i = 0; i < 16; ++i) {

0 commit comments

Comments
 (0)