From 0a54eef733c90d8b5cbe660211f55595c8fd5279 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Tue, 1 Dec 2026 14:05:32 +0800 Subject: [PATCH] fix: resolve silent truncation on non-EILSEQ iconv errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: In ChangeFileEncodingFormat(), the iconv do-while loop's else branch (for non-EILSEQ errors like EINVAL) performed a silent break, truncating all remaining content after the error byte 2. Fix: replace the break with the same recovery strategy used by the EILSEQ branch — skip the current input byte, append '?' to the output buffer, and continue the conversion loop instead of aborting 3. Impact: files containing invalid byte sequences (e.g. kern.log with 53263 lines showing only 1034) now load completely with '?' replacing invalid bytes instead of being silently truncated fix: 修复编码转换中非EILSEQ的iconv错误导致文件内容静默截断 1. 根因:ChangeFileEncodingFormat()的iconv循环中,else分支(处理EINVAL等 非EILSEQ错误)直接break,导致错误字节之后的所有内容被静默截断 2. 方案:将break替换为与EILSEQ分支相同的恢复策略——跳过当前输入字节、 在输出缓冲区追加'?'、继续循环而非中止转换 3. 影响:包含非法字节序列的文件(如kern.log 53263行仅显示1034行) 现在能完整加载,非法字节以'?'替代而非静默截断 Log: Fixed file content truncation when encountering invalid byte sequences during encoding conversion Influence: - Test opening files with invalid byte sequences to verify complete content display - Verify normal encoding files load without regression - Test UTF-8 to GB18030 and reverse conversion with mixed valid/invalid bytes Log: 修复包含非法字节序列的文件在编码转换时内容被静默截断的问题 Influence: - 测试打开含非法字节的文件,验证内容完整显示 - 验证正常编码文件加载无回归 - 测试UTF-8与GB18030互转场景下混合有效/非法字节的处理 PMS: BUG-384499 --- src/encodes/detectcode.cpp | 22 ++++++++++++++++++++-- tests/common2/test_detectcode.cpp | 8 ++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/encodes/detectcode.cpp b/src/encodes/detectcode.cpp index 25e818c5..a635b3cd 100644 --- a/src/encodes/detectcode.cpp +++ b/src/encodes/detectcode.cpp @@ -996,8 +996,26 @@ bool DetectCode::ChangeFileEncodingFormat(QByteArray &inputStr, inbytesleft -= replaceLen; qCDebug(appLog) << "Skipped" << replaceLen << "bytes from input, remaining inbytesleft:" << inbytesleft; } else { - qCWarning(appLog) << "[DetectCode] iconv_open failed for conversion from" << fromCode << "to" << toCode; - break; + qCWarning(appLog) << "[DetectCode] iconv conversion error for" << fromCode << "to" << toCode << "errno:" << errorNum; + if (outbytesleft == 0) { + qCWarning(appLog) << "Output buffer exhausted, breaking conversion"; + break; + } + if (outbytesleft < 1) { + qCWarning(appLog) << "Output buffer too small for replacement char, breaking"; + break; + } + outbytesleft -= 1; + *outbuf = '?'; + outbuf += 1; + qCDebug(appLog) << "Appended replacement char '?' for non-EILSEQ error, remaining outbytesleft:" << outbytesleft; + if (inbytesleft <= 1) { + inbuf += inbytesleft; + inbytesleft = 0; + break; + } + inbuf += 1; + inbytesleft -= 1; } } } while (static_cast(-1) == ret); diff --git a/tests/common2/test_detectcode.cpp b/tests/common2/test_detectcode.cpp index c7e56f2e..433746a6 100644 --- a/tests/common2/test_detectcode.cpp +++ b/tests/common2/test_detectcode.cpp @@ -876,15 +876,15 @@ TEST_F(DetectCodeTest, ChangeEncoding_InvalidUtf8Byte_ReplacedWithQuestionMark) EXPECT_EQ(out, QByteArrayLiteral("?")); } -TEST_F(DetectCodeTest, ChangeEncoding_TruncatedUtf8_KeepsConvertedPrefix) +TEST_F(DetectCodeTest, ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues) { - // Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 保留已转换前缀 + // Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 'a' 已转换,尾部 C3 替换为 '?' QByteArray in = QByteArrayLiteral("a") + QByteArray::fromHex("C3"), out; // Act const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("UTF-8"), QStringLiteral("GB18030")); - // Assert: 'a' 已转换,截断尾部放弃 + // Assert: 'a' 已转换,非法尾部字节替换为 '?' EXPECT_TRUE(ok); - EXPECT_EQ(out, QByteArrayLiteral("a")); + EXPECT_EQ(out, QByteArrayLiteral("a?")); } TEST_F(DetectCodeTest, ChangeEncoding_InvalidGb18030Byte_ReplacedAndContinues)