fix(run): keep a log line with a stray NUL byte out of UTF-16 decoding (#7390)

On Windows the console decoder took any read containing a NUL byte for UTF-16LE output (wsl.exe, cmd /u). A Spring Boot app logging a payload that ends in a NUL turned the whole read into CJK-looking garbage. A read is now UTF-16LE only when NUL bytes are dense (one byte in eight or more, as every ASCII character in UTF-16 has one) or one follows a line feed (every UTF-16 line end has one).

Fixes #7362
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 13:30:02 +08:00
1 джерело 93c15547ef
коміт 6bd2d5f843
1 file changed
+26 -3
+26 -3
Переглянути файл
@@ -112,8 +112,9 @@ enum Line {
/// one). The part of a line after its first non-ASCII byte is held until the line ends or
/// turns out not to be UTF-8; [`Self::release`] decodes a held prompt when no more output comes.
///
/// A read with NUL bytes, which text in the others never has, is UTF-16LE (ASCII characters
/// and line ends in it have them); UTF-16 goes on until one of its lines has ended and a read
/// A read with NUL bytes is UTF-16LE when they are many (every ASCII character in it has one)
/// or one follows a line feed (every line end has one): a stray NUL in a line of other text, such
/// as a binary payload a program logs, leaves the read as it is (#7362). UTF-16 goes on until one of its lines has ended and a read
/// without NUL bytes comes, or at once when a read without NUL bytes is text in UTF-8 again:
/// a program that writes a buffer whose tail is NULs leaves a UTF-16 line that never ends.
pub struct MixedDecoder {
@@ -147,7 +148,7 @@ impl MixedDecoder {
u.decode(bytes, out);
return;
}
if nul {
if nul && looks_utf16(bytes) {
self.end_line(out);
let mut u = Utf16::new();
u.decode(utf16_aligned(bytes), out);
@@ -259,6 +260,13 @@ impl Utf16 {
}
}
/// Whether a read with NUL bytes is UTF-16LE rather than text with a stray NUL: at least one byte
/// in eight is NUL, or a line feed is followed by one.
fn looks_utf16(bytes: &[u8]) -> bool {
let nuls = bytes.iter().filter(|b| **b == 0).count();
nuls * 8 >= bytes.len() || bytes.windows(2).any(|w| w == [b'\n', 0])
}
/// `bytes` from the first byte of a UTF-16LE code unit. NUL bytes are mostly the high bytes of
/// ASCII characters, at odd offsets from it; at even ones, the first byte is the high byte of a
/// character whose low byte came before (and was shown as ASCII).
@@ -468,6 +476,21 @@ mod encoding_tests {
assert!(out.ends_with("after-0: hello 正常输出\nafter-1: hello 正常输出\n"), "{out:?}");
}
/// A Spring Boot log line with ANSI colours whose logged payload ends in a NUL byte stays
/// UTF-8; taken for UTF-16LE, the whole read came out as CJK-looking garbage (#7362).
#[test]
fn a_stray_nul_in_a_utf8_line_is_not_utf16() {
let line = "\x1b[2m2026-12-02 10:55:31.427\x1b[0;39m \x1b[32m INFO\x1b[0;39m 28176 --- [ntContainer#1-5] \
\x1b[36mo.s.modules.data.ConsumerService\x1b[0;39m : Received LC message::: payload \
{\"funcId\":\"Log\",\"seq\":18004,\"messages\":[]}\0\r\n";
let next = "2026-12-02 10:55:31.493 INFO 28176 --- 处理完成 - 耗时: 66ms\r\n";
let mut d = mixed(936);
let mut out = String::new();
d.decode_into(format!("{line}{next}").as_bytes(), &mut out);
out.push_str(&d.finish());
assert_eq!(out, format!("{line}{next}"));
}
/// `水` is `34 6C` in UTF-16LE, which reads as `4l` in UTF-8: a read that short is not
/// taken for text again.
#[test]