Skip to content

Commit 4c1090c

Browse files
author
MarcoFalke
committed
Merge bitcoin#17279: refactor: Remove redundant c_str() calls in formatting
c72906d refactor: Remove redundant c_str() calls in formatting (Wladimir J. van der Laan) Pull request description: Our formatter, tinyformat, *never* needs `c_str()` for strings. Still, many places call it redundantly, resulting in longer code and a slight overhead. Remove redundant `c_str()` calls for: - `strprintf` - `LogPrintf` - `tfm::format` (also, combined with bitcoin#17095, I think this improves logging in case of unexpected embedded NULL characters) ACKs for top commit: ryanofsky: Code review ACK c72906d. Easy to review with `git log -p -n1 --word-diff-regex=. -U0 c72906d` Tree-SHA512: 9e21e7bed8aaff59b8b8aa11571396ddc265fb29608c2545b1fcdbbb36d65b37eb361db6688dd36035eab0c110f8de255375cfda50df3d9d7708bc092f67fefc
2 parents cfec3e0 + c72906d commit 4c1090c

File tree

14 files changed

+46
-46
lines changed

14 files changed

+46
-46
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main(int argc, char** argv)
3636
SetupBenchArgs();
3737
std::string error;
3838
if (!gArgs.ParseParameters(argc, argv, error)) {
39-
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
39+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
4040
return EXIT_FAILURE;
4141
}
4242

@@ -60,7 +60,7 @@ int main(int argc, char** argv)
6060

6161
double scaling_factor;
6262
if (!ParseDouble(scaling_str, &scaling_factor)) {
63-
tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
63+
tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str);
6464
return EXIT_FAILURE;
6565
}
6666

src/bitcoin-cli.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static int AppInitRPC(int argc, char* argv[])
105105
SetupCliArgs();
106106
std::string error;
107107
if (!gArgs.ParseParameters(argc, argv, error)) {
108-
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
108+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
109109
return EXIT_FAILURE;
110110
}
111111
if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
@@ -119,19 +119,19 @@ static int AppInitRPC(int argc, char* argv[])
119119
strUsage += "\n" + gArgs.GetHelpMessage();
120120
}
121121

122-
tfm::format(std::cout, "%s", strUsage.c_str());
122+
tfm::format(std::cout, "%s", strUsage);
123123
if (argc < 2) {
124124
tfm::format(std::cerr, "Error: too few parameters\n");
125125
return EXIT_FAILURE;
126126
}
127127
return EXIT_SUCCESS;
128128
}
129129
if (!CheckDataDirOption()) {
130-
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
130+
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
131131
return EXIT_FAILURE;
132132
}
133133
if (!gArgs.ReadConfigFiles(error, true)) {
134-
tfm::format(std::cerr, "Error reading configuration file: %s\n", error.c_str());
134+
tfm::format(std::cerr, "Error reading configuration file: %s\n", error);
135135
return EXIT_FAILURE;
136136
}
137137
// Check for -chain, -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
@@ -393,7 +393,7 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
393393
if (failedToGetAuthCookie) {
394394
throw std::runtime_error(strprintf(
395395
"Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (%s)",
396-
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str()));
396+
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)).string()));
397397
} else {
398398
throw std::runtime_error("Authorization failed: Incorrect rpcuser or rpcpassword");
399399
}
@@ -541,7 +541,7 @@ static int CommandLineRPC(int argc, char *argv[])
541541
}
542542

543543
if (strPrint != "") {
544-
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint.c_str());
544+
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint);
545545
}
546546
return nRet;
547547
}

src/bitcoin-tx.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int AppInitRawTx(int argc, char* argv[])
8383
SetupBitcoinTxArgs();
8484
std::string error;
8585
if (!gArgs.ParseParameters(argc, argv, error)) {
86-
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
86+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
8787
return EXIT_FAILURE;
8888
}
8989

@@ -105,7 +105,7 @@ static int AppInitRawTx(int argc, char* argv[])
105105
"\n";
106106
strUsage += gArgs.GetHelpMessage();
107107

108-
tfm::format(std::cout, "%s", strUsage.c_str());
108+
tfm::format(std::cout, "%s", strUsage);
109109

110110
if (argc < 2) {
111111
tfm::format(std::cerr, "Error: too few parameters\n");
@@ -724,21 +724,21 @@ static void OutputTxJSON(const CTransaction& tx)
724724
TxToUniv(tx, uint256(), entry);
725725

726726
std::string jsonOutput = entry.write(4);
727-
tfm::format(std::cout, "%s\n", jsonOutput.c_str());
727+
tfm::format(std::cout, "%s\n", jsonOutput);
728728
}
729729

730730
static void OutputTxHash(const CTransaction& tx)
731731
{
732732
std::string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
733733

734-
tfm::format(std::cout, "%s\n", strHexHash.c_str());
734+
tfm::format(std::cout, "%s\n", strHexHash);
735735
}
736736

737737
static void OutputTxHex(const CTransaction& tx)
738738
{
739739
std::string strHex = EncodeHexTx(tx);
740740

741-
tfm::format(std::cout, "%s\n", strHex.c_str());
741+
tfm::format(std::cout, "%s\n", strHex);
742742
}
743743

744744
static void OutputTx(const CTransaction& tx)
@@ -829,7 +829,7 @@ static int CommandLineRawTx(int argc, char* argv[])
829829
}
830830

831831
if (strPrint != "") {
832-
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint.c_str());
832+
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint);
833833
}
834834
return nRet;
835835
}

src/bitcoin-wallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static bool WalletAppInit(int argc, char* argv[])
3636
SetupWalletToolArgs();
3737
std::string error_message;
3838
if (!gArgs.ParseParameters(argc, argv, error_message)) {
39-
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message.c_str());
39+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
4040
return false;
4141
}
4242
if (argc < 2 || HelpRequested(gArgs)) {
@@ -48,15 +48,15 @@ static bool WalletAppInit(int argc, char* argv[])
4848
" bitcoin-wallet [options] <command>\n\n" +
4949
gArgs.GetHelpMessage();
5050

51-
tfm::format(std::cout, "%s", usage.c_str());
51+
tfm::format(std::cout, "%s", usage);
5252
return false;
5353
}
5454

5555
// check for printtoconsole, allow -debug
5656
LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
5757

5858
if (!CheckDataDirOption()) {
59-
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
59+
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
6060
return false;
6161
}
6262
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
@@ -87,7 +87,7 @@ int main(int argc, char* argv[])
8787
for(int i = 1; i < argc; ++i) {
8888
if (!IsSwitchChar(argv[i][0])) {
8989
if (!method.empty()) {
90-
tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method.c_str(), argv[i]);
90+
tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method, argv[i]);
9191
return EXIT_FAILURE;
9292
}
9393
method = argv[i];

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool AppInit(int argc, char* argv[])
7070
strUsage += "\n" + gArgs.GetHelpMessage();
7171
}
7272

73-
tfm::format(std::cout, "%s", strUsage.c_str());
73+
tfm::format(std::cout, "%s", strUsage);
7474
return true;
7575
}
7676

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ bool AppInitParameterInteraction()
943943
}
944944

945945
if (!fs::is_directory(GetBlocksDir())) {
946-
return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist.").translated, gArgs.GetArg("-blocksdir", "").c_str()));
946+
return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist.").translated, gArgs.GetArg("-blocksdir", "")));
947947
}
948948

949949
// parse and validate enabled filter types

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ static void ThreadMapPort()
14741474
if (externalIPAddress[0]) {
14751475
CNetAddr resolved;
14761476
if (LookupHost(externalIPAddress, resolved, false)) {
1477-
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1477+
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString());
14781478
AddLocal(resolved, LOCAL_UPNP);
14791479
}
14801480
} else {
@@ -2710,7 +2710,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
27102710
{
27112711
size_t nMessageSize = msg.data.size();
27122712
size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2713-
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2713+
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command), nMessageSize, pnode->GetId());
27142714

27152715
std::vector<unsigned char> serializedHeader;
27162716
serializedHeader.reserve(CMessageHeader::HEADER_SIZE);

src/noui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool noui_ThreadSafeMessageBox(const std::string& message, const std::string& ca
4545
if (!fSecure) {
4646
LogPrintf("%s%s\n", strCaption, message);
4747
}
48-
tfm::format(std::cerr, "%s%s\n", strCaption.c_str(), message.c_str());
48+
tfm::format(std::cerr, "%s%s\n", strCaption, message);
4949
return false;
5050
}
5151

@@ -96,4 +96,4 @@ void noui_reconnect()
9696
noui_ThreadSafeQuestionConn.disconnect();
9797
noui_InitMessageConn.disconnect();
9898
noui_connect();
99-
}
99+
}

src/policy/fees.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry& entry, boo
517517
uint256 hash = entry.GetTx().GetHash();
518518
if (mapMemPoolTxs.count(hash)) {
519519
LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy error mempool tx %s already being tracked\n",
520-
hash.ToString().c_str());
520+
hash.ToString());
521521
return;
522522
}
523523

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ UniValue joinpsbts(const JSONRPCRequest& request)
16021602
for (auto& psbt : psbtxs) {
16031603
for (unsigned int i = 0; i < psbt.tx->vin.size(); ++i) {
16041604
if (!merged_psbt.AddInput(psbt.tx->vin[i], psbt.inputs[i])) {
1605-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Input %s:%d exists in multiple PSBTs", psbt.tx->vin[i].prevout.hash.ToString().c_str(), psbt.tx->vin[i].prevout.n));
1605+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Input %s:%d exists in multiple PSBTs", psbt.tx->vin[i].prevout.hash.ToString(), psbt.tx->vin[i].prevout.n));
16061606
}
16071607
}
16081608
for (unsigned int i = 0; i < psbt.tx->vout.size(); ++i) {

0 commit comments

Comments
 (0)