scripted-diff: [wallet] Remove trailing newlines from one-line log messages #36397

Manually merged
maflcko merged 5 commits from 2610-log-format-string-trailing-newline into master 2026-10-20 07:56:42 +00:00 AGit
maflcko commented 2026-10-01 11:58:06 +00:00 (Migrated from github.com)

This is a low-prio style cleanup for years, but:

So I wonder if a simple scripted-diff can cover the bulk of the changes now, with hopefully little conflicts with reviewed pull requests.

(Removing the trailing newlines from the remaining log messages is trivial and can be done in a later pull)

This is a low-prio style cleanup for years, but: * It is tedious and possibly distracting to do it manually when touching the line for other reasons. * The topic recently came up again when discussing log injection (https://github.com/bitcoin/bitcoin/pull/35833#issuecomment-5113804332) So I wonder if a simple scripted-diff can cover the bulk of the changes now, with hopefully little conflicts with reviewed pull requests. (Removing the trailing newlines from the remaining log messages is trivial and can be done in a later pull)
DrahtBot commented 2026-10-01 11:58:18 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK l0rinc, sedited
Stale ACK willcl-ark

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #bitcoin-core/gui/939 (gui: show wallet name in progress messages by ShauryaaSharma)
  • #36536 (log: escape C1 control characters in messages by fametrano)
  • #36268 (refactor: prune unused semi-colons by fanquake)
  • #36031 (wallet: Remove mapMasterKeys and enforce that only one encryption key can exist by achow101)
  • #35444 (wallet: make descriptor SPKM mutex non-recursive by w0xlt)
  • #34909 (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)
  • #34778 (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky)
  • #34400 (wallet: parallel fast rescan (approx 8x speed up with 8 threads) by Eunovo)
  • #33033 (wallet, sqlite: Encapsulate SQLite statements in a RAII class by achow101)
  • #30343 (wallet, logging: Replace WalletLogPrintf() with LogInfo() by ryanofsky)
  • #29256 (log, refactor: Allow log macros to accept context arguments by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/wallet/wallet.cpp: Wallet passphrase changed to an nDeriveIterations of %i → Wallet passphrase changed; nDeriveIterations = %i [the current wording incorrectly suggests the passphrase was changed to an iteration count]
  • src/wallet/wallet.cpp: Encrypting Wallet with an nDeriveIterations of %i → Encrypting wallet with nDeriveIterations = %i [the current wording is grammatically broken]

2026-10-19 08:38:50

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36397 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36397.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36397#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [l0rinc](https://github.com/bitcoin/bitcoin/pull/36397#pullrequestreview-5575855118), [sedited](https://github.com/bitcoin/bitcoin/pull/36397#pullrequestreview-5582144459) | | Stale ACK | [willcl-ark](https://github.com/bitcoin/bitcoin/pull/36397#pullrequestreview-5426851704) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#bitcoin-core/gui/939](https://github.com/bitcoin-core/gui/pull/939) <sub><img src="https://drahtbot.space/ack_count/bitcoin-core/gui/939.svg"></sub> (gui: show wallet name in progress messages by ShauryaaSharma) * [#36536](https://github.com/bitcoin/bitcoin/pull/36536) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36536.svg"></sub> (log: escape C1 control characters in messages by fametrano) * [#36268](https://github.com/bitcoin/bitcoin/pull/36268) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36268.svg"></sub> (refactor: prune unused semi-colons by fanquake) * [#36031](https://github.com/bitcoin/bitcoin/pull/36031) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36031.svg"></sub> (wallet: Remove mapMasterKeys and enforce that only one encryption key can exist by achow101) * [#35444](https://github.com/bitcoin/bitcoin/pull/35444) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35444.svg"></sub> (wallet: make descriptor SPKM mutex non-recursive by w0xlt) * [#34909](https://github.com/bitcoin/bitcoin/pull/34909) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34909.svg"></sub> (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux) * [#34778](https://github.com/bitcoin/bitcoin/pull/34778) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34778.svg"></sub> (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky) * [#34400](https://github.com/bitcoin/bitcoin/pull/34400) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34400.svg"></sub> (wallet: parallel fast rescan (approx 8x speed up with 8 threads) by Eunovo) * [#33033](https://github.com/bitcoin/bitcoin/pull/33033) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33033.svg"></sub> (wallet, sqlite: Encapsulate SQLite statements in a RAII class by achow101) * [#30343](https://github.com/bitcoin/bitcoin/pull/30343) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/30343.svg"></sub> (wallet, logging: Replace WalletLogPrintf() with LogInfo() by ryanofsky) * [#29256](https://github.com/bitcoin/bitcoin/pull/29256) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/29256.svg"></sub> (log, refactor: Allow log macros to accept context arguments by ryanofsky) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### LLM Linter (✨ experimental) Possible typos and grammar issues: - `src/wallet/wallet.cpp`: `Wallet passphrase changed to an nDeriveIterations of %i` → `Wallet passphrase changed; nDeriveIterations = %i` [the current wording incorrectly suggests the passphrase was changed *to* an iteration count] - `src/wallet/wallet.cpp`: `Encrypting Wallet with an nDeriveIterations of %i` → `Encrypting wallet with nDeriveIterations = %i` [the current wording is grammatically broken] <sup>2026-10-19 08:38:50</sup>
First-time contributor

Base: 6301d464f72d2752867c974e747f7a78b4f8322f
Head: fa0c552cc5abdd0a7e7f2c9cfc1491240494f352

No verified findings are available from this review.

Review coverage was incomplete. adversarial_glm: Stage budget exhausted before the review completed.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `6301d464f72d2752867c974e747f7a78b4f8322f` Head: `fa0c552cc5abdd0a7e7f2c9cfc1491240494f352` No verified findings are available from this review. Review coverage was incomplete. adversarial_glm: Stage budget exhausted before the review completed. [Full review report](<https://review.fish.foo/traces/36397-fa0c552cc5abdd0a7e7f2c9cfc1491240494f352-891-5.html>)
maflcko commented 2026-10-01 12:10:16 +00:00 (Migrated from github.com)

39 conflicts right now. Not sure what number is acceptable, but the focus should probably be to not invalidate any current acks

39 conflicts right now. Not sure what number is acceptable, but the focus should probably be to not invalidate any current acks
willcl-ark commented 2026-10-01 12:33:53 +00:00 (Migrated from github.com)

39 conflicts right now. Not sure what number is acceptable, but the focus should probably be to not invalidate any current acks.

I've wondered this before, but am again wondering if there's a way to have this drahtbot comment also display how many current (full) ACKs are on each of these? IIRC previously I thought it may be a bit tricky/probably not worth it, but with LLMs maybe it might be possible to show?

I think 39 conflicts is probably a strong enough signal on it's own here, but when there's 5 or 6 it can be useful (but tedious) to click each one and actually see the state of it...

> 39 conflicts right now. Not sure what number is acceptable, but the focus should probably be to not invalidate any current acks. I've wondered this before, but am again wondering if there's a way to have this drahtbot comment also display how many current (full) ACKs are on each of these? IIRC previously I thought it may be a bit tricky/probably not worth it, but with LLMs maybe it might be possible to show? I think 39 conflicts is probably a strong enough signal on it's own here, but when there's 5 or 6 it can be useful (but tedious) to click each one and actually see the state of it...
l0rinc commented 2026-10-01 18:26:48 +00:00 (Migrated from github.com)

Concept ACK - could a linter check that we don't add new ones?

If we do these big messy refactors, they should be done at the beginning of the release cycle (i.e. soon), and maybe we could ping the people affected, and whoever reviews this could help re-ACK the invalidated ones.
+1 for adding valid ACK count to drahtbot.

Concept ACK - could a linter check that we don't add new ones? If we do these big messy refactors, they should be done at the beginning of the release cycle (i.e. soon), and maybe we could ping the people affected, and whoever reviews this could help re-ACK the invalidated ones. +1 for adding valid ACK count to drahtbot.
DrahtBot commented 2026-10-03 00:41:20 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
maflcko commented 2026-10-05 14:19:10 +00:00 (Migrated from github.com)

Concept ACK - could a linter check that we don't add new ones?

It should be checked at compile time, but this can be done later.

If we do these big messy refactors, they should be done at the beginning of the release cycle (i.e. soon)

I'd say no, because the beginning of the cycle is best used for features (gives them more time to sit). Also, backports are easier if refactors are done later.

> Concept ACK - could a linter check that we don't add new ones? It should be checked at compile time, but this can be done later. > If we do these big messy refactors, they should be done at the beginning of the release cycle (i.e. soon) I'd say no, because the beginning of the cycle is best used for features (gives them more time to sit). Also, backports are easier if refactors are done later.
DrahtBot commented 2026-10-05 16:02:59 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task Alpine (musl): https://github.com/bitcoin/bitcoin/actions/runs/37332770376/job/111839778318
LLM reason (✨ experimental): The build failed because wallet.cpp passes a format string ending in a newline to WalletLogPrintf, which the compile-time format check rejects.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `Alpine (musl)`: https://github.com/bitcoin/bitcoin/actions/runs/37332770376/job/111839778318</sub> <sub>LLM reason (✨ experimental): The build failed because `wallet.cpp` passes a format string ending in a newline to `WalletLogPrintf`, which the compile-time format check rejects.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
maflcko commented 2026-10-05 16:03:49 +00:00 (Migrated from github.com)

There are no reviewed wallet conflicts, so I limited the change to wallet for now. Also, enforced it at compile time.

There are no reviewed wallet conflicts, so I limited the change to wallet for now. Also, enforced it at compile time.
willcl-ark (Migrated from github.com) approved these changes 2026-10-06 10:10:36 +00:00
willcl-ark (Migrated from github.com) left a comment

ACK fa3da29147

I think ralphs suggestion here is legit, but I don't think it's worth fixing.

Agree with l0rinc that a lint check could be nice for this after the remainder are fixed.

ACK fa3da29147d52946155ad48e60deec8d0f18505d I think ralphs suggestion here is legit, but I don't think it's worth fixing. Agree with l0rinc that a lint check could be nice for this after the remainder are fixed.
maflcko commented 2026-10-06 11:33:24 +00:00 (Migrated from github.com)

I think ralphs suggestion here is legit, but I don't think it's worth fixing.

Heh, as Ralph says, there should be no production impact/change here, so this is still a refactor. In any case, avoiding a double-newline seems beneficial, so this is either irrelevant or this pull has it "fixed".

Agree with l0rinc that a lint check could be nice for this after the remainder are fixed.

Not sure I understand this. This is already rejected at compile time, see https://github.com/bitcoin/bitcoin/pull/36397#issuecomment-5998210751. So not sure what a redundant linter could achieve here?

> I think ralphs suggestion here is legit, but I don't think it's worth fixing. Heh, as Ralph says, there should be no production impact/change here, so this is still a refactor. In any case, avoiding a double-newline seems beneficial, so this is either irrelevant or this pull has it "fixed". > Agree with l0rinc that a lint check could be nice for this after the remainder are fixed. Not sure I understand this. This is already rejected at compile time, see https://github.com/bitcoin/bitcoin/pull/36397#issuecomment-5998210751. So not sure what a redundant linter could achieve here?
l0rinc (Migrated from github.com) reviewed 2026-10-17 14:48:38 +00:00
l0rinc (Migrated from github.com) left a comment

ACK fa3da29147, with a few optional suggestions to simplify the validation and tests.

ACK fa3da29147d52946155ad48e60deec8d0f18505d, with a few optional suggestions to simplify the validation and tests.
l0rinc (Migrated from github.com) commented 2026-10-17 14:47:38 +00:00

fa3da29 wallet: Reject trailing newline in format strings:

nit: I think we can make the concept arg a callable, which would let us inline the strings:

Inline the format strings as callables
diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp
index 73ee89ce54..17a54e101e 100644
--- a/src/test/util_string_tests.cpp
+++ b/src/test/util_string_tests.cpp
@@ -16,17 +16,12 @@ using util::detail::CheckNumFormatSpecifiers;
 
 BOOST_AUTO_TEST_SUITE(util_string_tests)
 
-namespace {
-constexpr char NO_TRAILING_NEWLINE[] = "format string without a trailing newline";
-constexpr char TRAILING_NEWLINE[] = "format string ending in a newline\n";
-
-template <const char* str>
+template <auto str>
 concept IsStrictFormatString =
-    requires { typename std::bool_constant<(ConstevalFormatString</*num_params=*/0, /*permit_trailing_newline=*/false>{str}, true)>; };
+    requires { typename std::bool_constant<(ConstevalFormatString</*num_params=*/0, /*permit_trailing_newline=*/false>{str()}, true)>; };
 
-static_assert(IsStrictFormatString<NO_TRAILING_NEWLINE>);
-static_assert(!IsStrictFormatString<TRAILING_NEWLINE>);
-} // namespace
+static_assert( IsStrictFormatString<[] { return "format string without a trailing newline"; }>);
+static_assert(!IsStrictFormatString<[] { return "format string ending in a newline\n"; }>);
 
 template <unsigned NumArgs>
 void TfmFormatZeroes(const std::string& fmt)
> [fa3da29](https://github.com/bitcoin/bitcoin/pull/36397/changes/fa3da29147d52946155ad48e60deec8d0f18505d) _wallet: Reject trailing newline in format strings_: nit: I think we can make the concept arg a callable, which would let us inline the strings: <details><summary>Inline the format strings as callables</summary> ```diff diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp index 73ee89ce54..17a54e101e 100644 --- a/src/test/util_string_tests.cpp +++ b/src/test/util_string_tests.cpp @@ -16,17 +16,12 @@ using util::detail::CheckNumFormatSpecifiers; BOOST_AUTO_TEST_SUITE(util_string_tests) -namespace { -constexpr char NO_TRAILING_NEWLINE[] = "format string without a trailing newline"; -constexpr char TRAILING_NEWLINE[] = "format string ending in a newline\n"; - -template <const char* str> +template <auto str> concept IsStrictFormatString = - requires { typename std::bool_constant<(ConstevalFormatString</*num_params=*/0, /*permit_trailing_newline=*/false>{str}, true)>; }; + requires { typename std::bool_constant<(ConstevalFormatString</*num_params=*/0, /*permit_trailing_newline=*/false>{str()}, true)>; }; -static_assert(IsStrictFormatString<NO_TRAILING_NEWLINE>); -static_assert(!IsStrictFormatString<TRAILING_NEWLINE>); -} // namespace +static_assert( IsStrictFormatString<[] { return "format string without a trailing newline"; }>); +static_assert(!IsStrictFormatString<[] { return "format string ending in a newline\n"; }>); template <unsigned NumArgs> void TfmFormatZeroes(const std::string& fmt) ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-17 14:29:03 +00:00

fa3da29 wallet: Reject trailing newline in format strings:

This seems a bit hacky to me. Adding a boolean on the general ConstevalFormatString leaks the newline check into tinyformat and index code. Please consider moving the validation into util::log::FormatString instead, where trailing newlines are already rejected.

Keep the newline check in logging
diff --git a/src/index/base.h b/src/index/base.h
index 9890e019da..731bbd26ba 100644
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -12,7 +12,6 @@
 #include <sync.h>
 #include <uint256.h>
 #include <util/fs.h>
-#include <util/string.h>
 #include <util/threadinterrupt.h>
 #include <validationinterface.h>
 
@@ -37,6 +36,11 @@ struct IndexSummary {
 namespace interfaces {
 struct BlockRef;
 }
+namespace util {
+template <unsigned int num_params>
+struct ConstevalFormatString;
+}
+
 /**
  * Base class for indices of blockchain data. This implements
  * CValidationInterface and ensures blocks are indexed sequentially according
diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp
index d71bc22ef9..505a40eab9 100644
--- a/src/test/logging_tests.cpp
+++ b/src/test/logging_tests.cpp
@@ -21,6 +21,7 @@
 #include <iostream>
 #include <source_location>
 #include <string>
+#include <type_traits>
 #include <unordered_map>
 #include <utility>
 #include <vector>
@@ -35,6 +36,12 @@ namespace BCLog {
     std::string LogEscapeMessage(std::string_view str);
 }
 
+template <auto str>
+concept IsLogFormatString = requires { typename std::bool_constant<(util::log::FormatString<0>{str()}, true)>; };
+
+static_assert( IsLogFormatString<[] { return "format string without a trailing newline"; }>);
+static_assert(!IsLogFormatString<[] { return "format string ending in a newline\n"; }>);
+
 BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup)
 
 static void ResetLogger()
diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp
index 73ee89ce54..894c592ed0 100644
--- a/src/test/util_string_tests.cpp
+++ b/src/test/util_string_tests.cpp
@@ -2,32 +2,18 @@
 // Distributed under the MIT software license, see the accompanying
 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
-#include <test/util/common.h>
-#include <tinyformat.h>
 #include <util/strencodings.h>
 #include <util/string.h>
 
 #include <boost/test/unit_test.hpp>
-
-#include <type_traits>
+#include <test/util/common.h>
+#include <tinyformat.h>
 
 using namespace util;
 using util::detail::CheckNumFormatSpecifiers;
 
 BOOST_AUTO_TEST_SUITE(util_string_tests)
 
-namespace {
-constexpr char NO_TRAILING_NEWLINE[] = "format string without a trailing newline";
-constexpr char TRAILING_NEWLINE[] = "format string ending in a newline\n";
-
-template <const char* str>
-concept IsStrictFormatString =
-    requires { typename std::bool_constant<(ConstevalFormatString</*num_params=*/0, /*permit_trailing_newline=*/false>{str}, true)>; };
-
-static_assert(IsStrictFormatString<NO_TRAILING_NEWLINE>);
-static_assert(!IsStrictFormatString<TRAILING_NEWLINE>);
-} // namespace
-
 template <unsigned NumArgs>
 void TfmFormatZeroes(const std::string& fmt)
 {
diff --git a/src/tinyformat.h b/src/tinyformat.h
index c21a7507b0..444cd6ded5 100644
--- a/src/tinyformat.h
+++ b/src/tinyformat.h
@@ -193,8 +193,7 @@ template <unsigned num_params>
 struct FormatStringCheck {
     consteval FormatStringCheck(const char* str) : fmt{util::ConstevalFormatString<num_params>{str}.fmt} {}
     FormatStringCheck(LIFETIMEBOUND const RuntimeFormat& run) : fmt{run.fmt.c_str()} {}
-    template <bool permit_trailing_newline>
-    FormatStringCheck(util::ConstevalFormatString<num_params, permit_trailing_newline> str) : fmt{str.fmt} {}
+    FormatStringCheck(util::ConstevalFormatString<num_params> str) : fmt{str.fmt} {}
     operator const char*() { return fmt; }
     const char* fmt;
 };
diff --git a/src/util/log.h b/src/util/log.h
index 33d0b91bdf..9dbbadc60d 100644
--- a/src/util/log.h
+++ b/src/util/log.h
@@ -80,6 +80,15 @@ bool ShouldTraceLog(Category category);
 /** Send message to be logged. Applications using the logging library need to provide this. */
 void Log(Entry entry);
 
+/** A format string for a log entry. The logger appends the trailing newline, so reject one in the format. */
+template <unsigned num_params>
+struct FormatString : util::ConstevalFormatString<num_params> {
+    consteval FormatString(const char* str) : util::ConstevalFormatString<num_params>{str}
+    {
+        if (std::string_view{str}.ends_with('\n')) throw "Format string must not end with a newline";
+    }
+};
+
 template <typename... Args>
 inline void LogPrintFormatInternal_(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, bool should_ratelimit, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
 {
diff --git a/src/util/string.h b/src/util/string.h
index 0809e84c8a..b2be2e7f64 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -92,16 +92,10 @@ constexpr void CheckNumFormatSpecifiers(const char* str)
  * run-time. Validation is partial to try and prevent the most common errors
  * while avoiding re-implementing the entire parsing logic.
  */
-template <unsigned num_params, bool permit_trailing_newline = true>
+template <unsigned num_params>
 struct ConstevalFormatString {
     const char* const fmt;
-    consteval ConstevalFormatString(const char* str) : fmt{str}
-    {
-        detail::CheckNumFormatSpecifiers<num_params>(fmt);
-        if constexpr (!permit_trailing_newline) {
-            if (std::string_view{fmt}.ends_with('\n')) throw "Format string must not end with a newline";
-        }
-    }
+    consteval ConstevalFormatString(const char* str) : fmt{str} { detail::CheckNumFormatSpecifiers<num_params>(fmt); }
 };
 
 /// Replace every non-overlapping occurrence of `search` with `substitute`, treating both literally; the replacement text is not searched again.
diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h
index 6343746239..f9b7303285 100644
--- a/src/wallet/scriptpubkeyman.h
+++ b/src/wallet/scriptpubkeyman.h
@@ -147,7 +147,7 @@ public:
 
     /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
     template <typename... Params>
-    void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params), /*permit_trailing_newline=*/false> wallet_fmt, const Params&... params) const
+    void WalletLogPrintf(util::log::FormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
     {
         LogInfo("[%s] %s", m_storage.LogName(), tfm::format(wallet_fmt, params...));
     };
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index ad67aa0b4b..350d7e4db1 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -923,7 +923,7 @@ public:
 
     /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
     template <typename... Params>
-    void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params), /*permit_trailing_newline=*/false> wallet_fmt, const Params&... params) const
+    void WalletLogPrintf(util::log::FormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
     {
         LogInfo("[%s] %s", LogName(), tfm::format(wallet_fmt, params...));
     };
> [fa3da29](https://github.com/bitcoin/bitcoin/pull/36397/changes/fa3da29147d52946155ad48e60deec8d0f18505d) _wallet: Reject trailing newline in format strings_: This seems a bit hacky to me. Adding a boolean on the general `ConstevalFormatString` leaks the newline check into tinyformat and index code. Please consider moving the validation into `util::log::FormatString` instead, where trailing newlines are already rejected. <details><summary>Keep the newline check in logging</summary> ```diff diff --git a/src/index/base.h b/src/index/base.h index 9890e019da..731bbd26ba 100644 --- a/src/index/base.h +++ b/src/index/base.h @@ -12,7 +12,6 @@ #include <sync.h> #include <uint256.h> #include <util/fs.h> -#include <util/string.h> #include <util/threadinterrupt.h> #include <validationinterface.h> @@ -37,6 +36,11 @@ struct IndexSummary { namespace interfaces { struct BlockRef; } +namespace util { +template <unsigned int num_params> +struct ConstevalFormatString; +} + /** * Base class for indices of blockchain data. This implements * CValidationInterface and ensures blocks are indexed sequentially according diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp index d71bc22ef9..505a40eab9 100644 --- a/src/test/logging_tests.cpp +++ b/src/test/logging_tests.cpp @@ -21,6 +21,7 @@ #include <iostream> #include <source_location> #include <string> +#include <type_traits> #include <unordered_map> #include <utility> #include <vector> @@ -35,6 +36,12 @@ namespace BCLog { std::string LogEscapeMessage(std::string_view str); } +template <auto str> +concept IsLogFormatString = requires { typename std::bool_constant<(util::log::FormatString<0>{str()}, true)>; }; + +static_assert( IsLogFormatString<[] { return "format string without a trailing newline"; }>); +static_assert(!IsLogFormatString<[] { return "format string ending in a newline\n"; }>); + BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup) static void ResetLogger() diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp index 73ee89ce54..894c592ed0 100644 --- a/src/test/util_string_tests.cpp +++ b/src/test/util_string_tests.cpp @@ -2,32 +2,18 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include <test/util/common.h> -#include <tinyformat.h> #include <util/strencodings.h> #include <util/string.h> #include <boost/test/unit_test.hpp> - -#include <type_traits> +#include <test/util/common.h> +#include <tinyformat.h> using namespace util; using util::detail::CheckNumFormatSpecifiers; BOOST_AUTO_TEST_SUITE(util_string_tests) -namespace { -constexpr char NO_TRAILING_NEWLINE[] = "format string without a trailing newline"; -constexpr char TRAILING_NEWLINE[] = "format string ending in a newline\n"; - -template <const char* str> -concept IsStrictFormatString = - requires { typename std::bool_constant<(ConstevalFormatString</*num_params=*/0, /*permit_trailing_newline=*/false>{str}, true)>; }; - -static_assert(IsStrictFormatString<NO_TRAILING_NEWLINE>); -static_assert(!IsStrictFormatString<TRAILING_NEWLINE>); -} // namespace - template <unsigned NumArgs> void TfmFormatZeroes(const std::string& fmt) { diff --git a/src/tinyformat.h b/src/tinyformat.h index c21a7507b0..444cd6ded5 100644 --- a/src/tinyformat.h +++ b/src/tinyformat.h @@ -193,8 +193,7 @@ template <unsigned num_params> struct FormatStringCheck { consteval FormatStringCheck(const char* str) : fmt{util::ConstevalFormatString<num_params>{str}.fmt} {} FormatStringCheck(LIFETIMEBOUND const RuntimeFormat& run) : fmt{run.fmt.c_str()} {} - template <bool permit_trailing_newline> - FormatStringCheck(util::ConstevalFormatString<num_params, permit_trailing_newline> str) : fmt{str.fmt} {} + FormatStringCheck(util::ConstevalFormatString<num_params> str) : fmt{str.fmt} {} operator const char*() { return fmt; } const char* fmt; }; diff --git a/src/util/log.h b/src/util/log.h index 33d0b91bdf..9dbbadc60d 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -80,6 +80,15 @@ bool ShouldTraceLog(Category category); /** Send message to be logged. Applications using the logging library need to provide this. */ void Log(Entry entry); +/** A format string for a log entry. The logger appends the trailing newline, so reject one in the format. */ +template <unsigned num_params> +struct FormatString : util::ConstevalFormatString<num_params> { + consteval FormatString(const char* str) : util::ConstevalFormatString<num_params>{str} + { + if (std::string_view{str}.ends_with('\n')) throw "Format string must not end with a newline"; + } +}; + template <typename... Args> inline void LogPrintFormatInternal_(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, bool should_ratelimit, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args) { diff --git a/src/util/string.h b/src/util/string.h index 0809e84c8a..b2be2e7f64 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -92,16 +92,10 @@ constexpr void CheckNumFormatSpecifiers(const char* str) * run-time. Validation is partial to try and prevent the most common errors * while avoiding re-implementing the entire parsing logic. */ -template <unsigned num_params, bool permit_trailing_newline = true> +template <unsigned num_params> struct ConstevalFormatString { const char* const fmt; - consteval ConstevalFormatString(const char* str) : fmt{str} - { - detail::CheckNumFormatSpecifiers<num_params>(fmt); - if constexpr (!permit_trailing_newline) { - if (std::string_view{fmt}.ends_with('\n')) throw "Format string must not end with a newline"; - } - } + consteval ConstevalFormatString(const char* str) : fmt{str} { detail::CheckNumFormatSpecifiers<num_params>(fmt); } }; /// Replace every non-overlapping occurrence of `search` with `substitute`, treating both literally; the replacement text is not searched again. diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index 6343746239..f9b7303285 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -147,7 +147,7 @@ public: /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */ template <typename... Params> - void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params), /*permit_trailing_newline=*/false> wallet_fmt, const Params&... params) const + void WalletLogPrintf(util::log::FormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const { LogInfo("[%s] %s", m_storage.LogName(), tfm::format(wallet_fmt, params...)); }; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index ad67aa0b4b..350d7e4db1 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -923,7 +923,7 @@ public: /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */ template <typename... Params> - void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params), /*permit_trailing_newline=*/false> wallet_fmt, const Params&... params) const + void WalletLogPrintf(util::log::FormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const { LogInfo("[%s] %s", LogName(), tfm::format(wallet_fmt, params...)); }; ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-17 14:00:08 +00:00

fa3da29 wallet: Reject trailing newline in format strings:

nit: Ralph may be right (but even if not), GetNewPrefixCursor can probably reach the wallet log - if you push again, consider dropping the suffix from both cursor setup errors too

Remove the cursor error newlines
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp
index fa2abc93a5..c72b77779d 100644
--- a/src/wallet/sqlite.cpp
+++ b/src/wallet/sqlite.cpp
@@ -613,7 +613,7 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewCursor()
     int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr);
     if (res != SQLITE_OK) {
         throw std::runtime_error(strprintf(
-            "%s: Failed to setup cursor SQL statement: %s\n", __func__, sqlite3_errstr(res)));
+            "%s: Failed to setup cursor SQL statement: %s", __func__, sqlite3_errstr(res)));
     }
 
     return cursor;
@@ -650,7 +650,7 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(std::span<const
     int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr);
     if (res != SQLITE_OK) {
         throw std::runtime_error(strprintf(
-            "SQLiteDatabase: Failed to setup cursor SQL statement: %s\n", sqlite3_errstr(res)));
+            "SQLiteDatabase: Failed to setup cursor SQL statement: %s", sqlite3_errstr(res)));
     }
 
     if (!BindBlobToStatement(cursor->m_cursor_stmt, 1, cursor->m_prefix_range_start, "prefix_start")) return nullptr;
> [fa3da29](https://github.com/bitcoin/bitcoin/pull/36397/changes/fa3da29147d52946155ad48e60deec8d0f18505d) _wallet: Reject trailing newline in format strings_: nit: Ralph may be right (but even if not), `GetNewPrefixCursor` can probably reach the wallet log - if you push again, consider dropping the suffix from both cursor setup errors too <details><summary>Remove the cursor error newlines</summary> ```diff diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index fa2abc93a5..c72b77779d 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -613,7 +613,7 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewCursor() int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr); if (res != SQLITE_OK) { throw std::runtime_error(strprintf( - "%s: Failed to setup cursor SQL statement: %s\n", __func__, sqlite3_errstr(res))); + "%s: Failed to setup cursor SQL statement: %s", __func__, sqlite3_errstr(res))); } return cursor; @@ -650,7 +650,7 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(std::span<const int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr); if (res != SQLITE_OK) { throw std::runtime_error(strprintf( - "SQLiteDatabase: Failed to setup cursor SQL statement: %s\n", sqlite3_errstr(res))); + "SQLiteDatabase: Failed to setup cursor SQL statement: %s", sqlite3_errstr(res))); } if (!BindBlobToStatement(cursor->m_cursor_stmt, 1, cursor->m_prefix_range_start, "prefix_start")) return nullptr; ``` </details>
sedited commented 2026-10-19 07:36:32 +00:00 (Migrated from github.com)

@maflcko do you want to take l0rinc's nits?

@maflcko do you want to take l0rinc's nits?
maflcko commented 2026-10-19 08:43:56 +00:00 (Migrated from github.com)

Rebased (for the scripted-diff), and pushed the nits, which seemed nice and hopefully make Ralph Review happy.

Rebased (for the scripted-diff), and pushed the nits, which seemed nice and hopefully make Ralph Review happy.
maflcko (Migrated from github.com) reviewed 2026-10-19 08:44:02 +00:00
maflcko (Migrated from github.com) commented 2026-10-19 08:44:02 +00:00

heh, nice code-golf. Pushed.

heh, nice code-golf. Pushed.
maflcko (Migrated from github.com) reviewed 2026-10-19 08:44:05 +00:00
maflcko (Migrated from github.com) commented 2026-10-19 08:44:05 +00:00

Yeah, I guess formatting itself shouldn't care about newlines and this is log specific. Pushed your diff and added you as co-author.

Yeah, I guess formatting itself shouldn't care about newlines and this is log specific. Pushed your diff and added you as co-author.
maflcko (Migrated from github.com) reviewed 2026-10-19 08:44:25 +00:00
maflcko (Migrated from github.com) commented 2026-10-19 08:44:25 +00:00

Hmm, if this is changed, it should be changed for all strings all at once. Pushed a commit to do this.

Hmm, if this is changed, it should be changed for all strings all at once. Pushed a commit to do this.
l0rinc (Migrated from github.com) approved these changes 2026-10-19 18:37:03 +00:00
l0rinc (Migrated from github.com) left a comment

ACK fa0c552cc5

Reviewed the diff and re-reviewed the commits manually, verified the scripted diff, and ran the tests locally.

ACK fa0c552cc5abdd0a7e7f2c9cfc1491240494f352 Reviewed the diff and re-reviewed the commits manually, verified the scripted diff, and ran the tests locally.
l0rinc (Migrated from github.com) commented 2026-10-19 18:21:15 +00:00

nit: based on other cases, this was probably meant to end with an ellipsis - if you touch this again:

    wallet->WalletLogPrintf("Releasing wallet %s...", name);
nit: based on [other cases](https://github.com/bitcoin/bitcoin/pull/36397/changes#diff-aa1531ddffd69dd6e0ae3550fc4dbac57967e6db05c965e5c9650a44f48c077aR230), this was probably meant to end with an ellipsis - if you touch this again: ```suggestion wallet->WalletLogPrintf("Releasing wallet %s...", name); ```
sedited (Migrated from github.com) approved these changes 2026-10-20 07:46:18 +00:00
sedited (Migrated from github.com) left a comment

ACK fa0c552cc5

This is low prio, but it does improve something that has been annoying me. Looks like there are fewer conflicts than before, and it only seems to invalidate single ack at the moment, so I'm putting this in now. I don't think there will be an ideal moment for this.

ACK fa0c552cc5abdd0a7e7f2c9cfc1491240494f352 This is low prio, but it does improve something that has been annoying me. Looks like there are fewer conflicts than before, and it only seems to invalidate single ack at the moment, so I'm putting this in now. I don't think there will be an ideal moment for this.
maflcko commented 2026-10-20 08:27:14 +00:00 (Migrated from github.com)

This is low prio, but it does improve something that has been annoying me.

Same. To clarify why I never addressed this for years, but created this pull now: After #35833, there now also a bunch of logging bugs with embedded \x0a in the log lines, like this (from running feature_filelock.py on previous master)

 node1 2026-10-20T08:20:45.649666Z [init] [wallet/sqlite.cpp:55] [TraceSqlCallback] [walletdb:trace] [/tmp/bitcoin_func_test_a43_hxup/node0/regtest/wallets/clezcl/wallet.dat] SQLite Statement: BEGIN EXCLUSIVE TRANSACTION 
 node1 2026-10-20T08:20:45.649707Z [init] [noui.cpp:29] [noui_ThreadSafeMessageBox] [error] SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core?\x0a 
 node1 2026-10-20T08:20:45.649719Z [init] [httpserver.cpp:1403] [InterruptHTTPServer] [http] Interrupting HTTP server 

This is harmless, but looks a bit like corrupt logs, so it would be good to fix, which this pull did.

There are still a few follow-ups and I'll try to fan them out over time.

> This is low prio, but it does improve something that has been annoying me. Same. To clarify why I never addressed this for years, but created this pull now: After #35833, there now also a bunch of logging bugs with embedded \x0a in the log lines, like this (from running `feature_filelock.py` on previous master) ``` node1 2026-10-20T08:20:45.649666Z [init] [wallet/sqlite.cpp:55] [TraceSqlCallback] [walletdb:trace] [/tmp/bitcoin_func_test_a43_hxup/node0/regtest/wallets/clezcl/wallet.dat] SQLite Statement: BEGIN EXCLUSIVE TRANSACTION node1 2026-10-20T08:20:45.649707Z [init] [noui.cpp:29] [noui_ThreadSafeMessageBox] [error] SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core?\x0a node1 2026-10-20T08:20:45.649719Z [init] [httpserver.cpp:1403] [InterruptHTTPServer] [http] Interrupting HTTP server ``` This is harmless, but looks a bit like corrupt logs, so it would be good to fix, which this pull did. There are still a few follow-ups and I'll try to fan them out over time.
willcl-ark (Migrated from github.com) approved these changes 2026-10-20 08:39:10 +00:00
willcl-ark (Migrated from github.com) left a comment

Post-merge reACK fa0c552cc5

Post-merge reACK fa0c552cc5abdd0a7e7f2c9cfc1491240494f352
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36397
No description provided.