Skip to content

Commit fa6dc57

Browse files
author
MarcoFalke
committed
refactor: Enforce C-str fmt strings in WalletLogPrintf()
1 parent fa244f3 commit fa6dc57

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

contrib/devtools/bitcoin-tidy/example_logprintf.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class CWallet
3737

3838
public:
3939
template <typename... Params>
40-
void WalletLogPrintf(std::string fmt, Params... parameters) const
40+
void WalletLogPrintf(const char* fmt, Params... parameters) const
4141
{
42-
LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);
42+
LogPrintf(("%s " + std::string{fmt}).c_str(), GetDisplayName(), parameters...);
4343
};
4444
};
4545

src/wallet/scriptpubkeyman.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,10 @@ class ScriptPubKeyMan
249249
virtual std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const { return {}; };
250250

251251
/** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
252-
template<typename... Params>
253-
void WalletLogPrintf(std::string fmt, Params... parameters) const {
254-
LogPrintf(("%s " + fmt).c_str(), m_storage.GetDisplayName(), parameters...);
252+
template <typename... Params>
253+
void WalletLogPrintf(const char* fmt, Params... parameters) const
254+
{
255+
LogPrintf(("%s " + std::string{fmt}).c_str(), m_storage.GetDisplayName(), parameters...);
255256
};
256257

257258
/** Watch-only address added */

src/wallet/wallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2319,7 +2319,7 @@ OutputType CWallet::TransactionChangeType(const std::optional<OutputType>& chang
23192319
void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm)
23202320
{
23212321
LOCK(cs_wallet);
2322-
WalletLogPrintf("CommitTransaction:\n%s", tx->ToString());
2322+
WalletLogPrintf("CommitTransaction:\n%s", tx->ToString()); // NOLINT(bitcoin-unterminated-logprintf)
23232323

23242324
// Add tx to wallet, because if it has change it's also ours,
23252325
// otherwise just for transaction history.

src/wallet/wallet.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
890890
};
891891

892892
/** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
893-
template<typename... Params>
894-
void WalletLogPrintf(std::string fmt, Params... parameters) const {
895-
LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);
893+
template <typename... Params>
894+
void WalletLogPrintf(const char* fmt, Params... parameters) const
895+
{
896+
LogPrintf(("%s " + std::string{fmt}).c_str(), GetDisplayName(), parameters...);
896897
};
897898

898899
/** Upgrade the wallet */

test/lint/run-lint-format-strings.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
2121
("src/test/translation_tests.cpp", "strprintf(format, arg)"),
2222
("src/validationinterface.cpp", "LogPrint(BCLog::VALIDATION, fmt \"\\n\", __VA_ARGS__)"),
23-
("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
24-
("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"),
25-
("src/wallet/scriptpubkeyman.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
26-
("src/wallet/scriptpubkeyman.h", "LogPrintf((\"%s \" + fmt).c_str(), m_storage.GetDisplayName(), parameters...)"),
23+
("src/wallet/wallet.h", "WalletLogPrintf(const char* fmt, Params... parameters)"),
24+
("src/wallet/wallet.h", "LogPrintf((\"%s \" + std::string{fmt}).c_str(), GetDisplayName(), parameters...)"),
25+
("src/wallet/scriptpubkeyman.h", "WalletLogPrintf(const char* fmt, Params... parameters)"),
26+
("src/wallet/scriptpubkeyman.h", "LogPrintf((\"%s \" + std::string{fmt}).c_str(), m_storage.GetDisplayName(), parameters...)"),
2727
]
2828

2929

0 commit comments

Comments
 (0)