log: escape C1 control characters in messages #36536

Open
fametrano wants to merge 1 commit from logging-escape-c1 into master AGit
fametrano commented 2026-10-15 07:10:08 +00:00 (Migrated from github.com)

The log escapes control characters so that a message cannot drive the terminal it is printed on, but it lets through the UTF-8 form of the C1 controls U+0080 to U+009F. VTE, the library behind GNOME Terminal and others, decodes these as CSI, OSC, DCS and the rest, like their ESC [, ESC ] and ESC P forms.

I escape the two-byte sequences 0xC2 0x80 to 0xC2 0x9F as \xc2\x9b and so on. Since 0xC2 is never a continuation byte, this needs no UTF-8 decoder, and all other non-ASCII text still passes untouched, as asked in #17095 and #35833. Single bytes 0x80 to 0x9F stay as they are, since they occur inside valid UTF-8. The new cases in test_LogEscapeMessage fail on master and pass with the change.

The log escapes control characters so that a message cannot drive the terminal it is printed on, but it lets through the UTF-8 form of the C1 controls U+0080 to U+009F. VTE, the library behind GNOME Terminal and others, decodes these as CSI, OSC, DCS and the rest, like their `ESC [`, `ESC ]` and `ESC P` forms. I escape the two-byte sequences 0xC2 0x80 to 0xC2 0x9F as `\xc2\x9b` and so on. Since 0xC2 is never a continuation byte, this needs no UTF-8 decoder, and all other non-ASCII text still passes untouched, as asked in #17095 and #35833. Single bytes 0x80 to 0x9F stay as they are, since they occur inside valid UTF-8. The new cases in `test_LogEscapeMessage` fail on master and pass with the change.
DrahtBot commented 2026-10-15 07:10:19 +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 jpk68, Naomi-Gift, cyb3ralbert

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

Conflicts

No conflicts as of last run.

<!--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/36536 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36536.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36536#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 | [jpk68](https://github.com/bitcoin/bitcoin/pull/36536#issuecomment-6329253274), [Naomi-Gift](https://github.com/bitcoin/bitcoin/pull/36536#issuecomment-6334301209), [cyb3ralbert](https://github.com/bitcoin/bitcoin/pull/36536#issuecomment-6336287931) | 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 No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: de96f3ae6d163e97a2cf29bbd7c2bf2d56e11019
Head: 40bb43f158bea7e2efdecff0e17d59d9bf32b924

No verified findings are available from this review.

Review coverage was incomplete.

Review limitations
  • verifier: The checkout confirms the changed escaping behavior and its use in log formatting, but does not contain VTE or another terminal implementation that independently verifies interpretation of the UTF-8 C1 sequences.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:0c398e5fd579cb8b00b6b62f2903ad741ac3f867996353a69082cd6ac57373b5:9d9e25a7349f2e108262b6ed44577d47304e975faf1917f1972defa0a828ae3d --> Base: `de96f3ae6d163e97a2cf29bbd7c2bf2d56e11019` Head: `40bb43f158bea7e2efdecff0e17d59d9bf32b924` No verified findings are available from this review. Review coverage was incomplete. <details> <summary>Review limitations</summary> - verifier: The checkout confirms the changed escaping behavior and its use in log formatting, but does not contain VTE or another terminal implementation that independently verifies interpretation of the UTF-8 C1 sequences. </details> [Full review report](<https://review.fish.foo/traces/36536-40bb43f158bea7e2efdecff0e17d59d9bf32b924-1034-3.html>)
DrahtBot commented 2026-10-20 08:25:50 +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).
jpk68 commented 2026-10-21 23:57:58 +00:00 (Migrated from github.com)
ACK 40bb43f158bea7e2efdecff0e17d59d9bf32b924
Naomi-Gift commented 2026-10-22 06:26:51 +00:00 (Migrated from github.com)

tACK 40bb43f158
test_LogEscapeMessage passes

tACK 40bb43f158bea7e2efdecff0e17d59d9bf32b924 test_LogEscapeMessage passes
cyb3ralbert commented 2026-10-22 08:26:44 +00:00 (Migrated from github.com)

tACK 40bb43f158

Tested on Linux x86_64 (Ubuntu 24.04), g++ 13.3.0, Release: logging_tests pass, also with this branch merged onto current master (de96f3ae). With U+009B and U+009F in a regtest datadir path, debug.log has \xc2\x9b and \xc2\x9f on this branch and the raw bytes on the merge-base.

Nit: the two new checks don't independently pin down both boundaries. Changing the lower bound to >= 0x7f or the end check to i + 1 <= str.size() still passes them. The trailing \xc2 in the second check is a string literal, so str[str.size()] reads the terminating NUL.

Adding after line 141:

BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("\xc2\x7f"), "\xc2" R"(\x7f)");
BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage(std::string_view{"\xc2\x9b", 1}), "\xc2");

Both pass on this branch. Each fails only with its corresponding mutant (lines 142 and 143, respectively; Debian 13 x86_64, GCC 14.2.0, Release).

Nit: the LogEscapeMessage doc comment and the test comment "ASCII and UTF-8 must pass through unaltered" don't mention the C1 exception. Also, U+0100 isn't adjacent to the tested range: that case checks a different lead byte followed by 0x80. The test itself is fine; only the comment is misleading.

tACK 40bb43f158bea7e2efdecff0e17d59d9bf32b924 Tested on Linux x86_64 (Ubuntu 24.04), g++ 13.3.0, Release: `logging_tests` pass, also with this branch merged onto current master (de96f3ae). With U+009B and U+009F in a regtest datadir path, debug.log has `\xc2\x9b` and `\xc2\x9f` on this branch and the raw bytes on the merge-base. Nit: the two new checks don't independently pin down both boundaries. Changing the lower bound to `>= 0x7f` or the end check to `i + 1 <= str.size()` still passes them. The trailing `\xc2` in the second check is a string literal, so `str[str.size()]` reads the terminating NUL. Adding after line 141: ```cpp BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("\xc2\x7f"), "\xc2" R"(\x7f)"); BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage(std::string_view{"\xc2\x9b", 1}), "\xc2"); ``` Both pass on this branch. Each fails only with its corresponding mutant (lines 142 and 143, respectively; Debian 13 x86_64, GCC 14.2.0, Release). Nit: the `LogEscapeMessage` doc comment and the test comment "ASCII and UTF-8 must pass through unaltered" don't mention the C1 exception. Also, U+0100 isn't adjacent to the tested range: that case checks a different lead byte followed by `0x80`. The test itself is fine; only the comment is misleading.
fametrano commented 2026-11-01 18:47:47 +00:00 (Migrated from github.com)

@cyb3ralbert thanks, both nits are right: the two extra checks pin each boundary, and the comments should mention the C1 exception. I will apply them in a push if a maintainer prefers, and otherwise leave the head as it is, so the three reviews of 40bb43f158 stay valid.

@cyb3ralbert thanks, both nits are right: the two extra checks pin each boundary, and the comments should mention the C1 exception. I will apply them in a push if a maintainer prefers, and otherwise leave the head as it is, so the three reviews of 40bb43f158 stay valid.
Sign in to join this conversation.
No reviewers
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!36536
No description provided.