From 94913a9f5ada47ec714933d014a9e206e7ebec61 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Tue, 27 Aug 2024 17:34:56 +0300 Subject: [PATCH 1/2] cxxrtl: use octal encoding of non-printables. "\x0a" is a perfectly valid escape sequence, but unfortunately "\x0ac" is equivalent to "\xac", and not "\x0a" "c" as we might expect --- *any* number of hexadecimal characters after the "\x" is accepted. This can be hit pretty easily if a newline is present in a format string. "\x{...}" syntax is only available as of C++23, so use octal format instead; a maximum of 3 digits following the backslash is accepted. The alternative would be to render every escape like `" "\x0a" "`, but it seems more effort that way. --- kernel/fmt.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/fmt.cc b/kernel/fmt.cc index d1c6b8ac9..44ad8351d 100644 --- a/kernel/fmt.cc +++ b/kernel/fmt.cc @@ -634,10 +634,11 @@ std::string escape_cxx_string(const std::string &input) output.push_back('\\'); output.push_back(c); } else { - char l = c & 0xf, h = (c >> 4) & 0xf; - output.append("\\x"); - output.push_back((h < 10 ? '0' + h : 'a' + h - 10)); - output.push_back((l < 10 ? '0' + l : 'a' + l - 10)); + char l = c & 0x7, m = (c >> 3) & 0x7, h = (c >> 6) & 0x3; + output.push_back('\\'); + output.push_back('0' + h); + output.push_back('0' + m); + output.push_back('0' + l); } } output.push_back('"'); From d0da1b56beb90f9633ed58cf0b52ea79d979ac86 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Tue, 27 Aug 2024 18:36:43 +0300 Subject: [PATCH 2/2] cxxrtl: backend: don't drop bits 2 and 5 on non-printable format. --- backends/cxxrtl/cxxrtl_backend.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backends/cxxrtl/cxxrtl_backend.cc b/backends/cxxrtl/cxxrtl_backend.cc index 8dc14863d..7e7bace6c 100644 --- a/backends/cxxrtl/cxxrtl_backend.cc +++ b/backends/cxxrtl/cxxrtl_backend.cc @@ -616,7 +616,7 @@ std::string escape_c_string(const std::string &input) output.push_back('\\'); output.push_back(c); } else { - char l = c & 0x3, m = (c >> 3) & 0x3, h = (c >> 6) & 0x3; + char l = c & 0x7, m = (c >> 3) & 0x7, h = (c >> 6) & 0x3; output.append("\\"); output.push_back('0' + h); output.push_back('0' + m);