Skip to content

log: escape C1 control characters in messages - #36536

Open
fametrano wants to merge 1 commit into
bitcoin:masterfrom
fametrano:logging-escape-c1
Open

fametrano wants to merge 1 commit into
bitcoin:masterfrom
fametrano:logging-escape-c1

Conversation

@fametrano

Copy link
Copy Markdown

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

DrahtBot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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.

LogEscapeMessage escapes bytes below 0x20 and 0x7f, but passes the
UTF-8 encodings of U+0080 to U+009F (0xC2 0x80 to 0xC2 0x9F), which
include CSI, OSC and DCS. VTE-based terminals, such as GNOME Terminal,
act on them.

Escape these two-byte sequences as well. 0xC2 is never a continuation
byte, so no decoder state is needed, and every other UTF-8 sequence
still passes through unaltered.
@jpk68

jpk68 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

ACK 40bb43f

@Naomi-Gift

Copy link
Copy Markdown

tACK 40bb43f
test_LogEscapeMessage passes

@cyb3ralbert

Copy link
Copy Markdown
Contributor

tACK 40bb43f

Tested on Linux x86_64 (Ubuntu 24.04), g++ 13.3.0, Release: logging_tests pass, also with this branch merged onto current master (de96f3a). 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.

@fametrano

Copy link
Copy Markdown
Author

@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 40bb43f stay valid.

@sedited
sedited requested a review from stickies-v November 5, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants