From 1515e5ac2e64739bd92eae26ca21a0ce34835f3f Mon Sep 17 00:00:00 2001 From: dengzhongyuan365-dev Date: Tue, 17 Nov 2026 18:44:03 +0800 Subject: [PATCH] fix: trust ICU over uchardet in encoding cross-validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: selectCoding() fallback path blindly returns uchardet result when it conflicts with ICU detection, causing TIS-620 misjudgment of UTF-8 Chinese HTML files 2. Fix: add cross-validation, when uchardet and ICU[0] are completely inconsistent and ICU[0] is UTF-8, trust ICU[0] over uchardet 3. Impact: only affects fallback path where detectors disagree; normal paths (containment match, GB18030 priority) unchanged Log: Fix content loss when opening UTF-8 files misdetected as TIS-620 Influence: 1. Test opening UTF-8 Chinese HTML files that trigger uchardet TIS-620 2. Test GB18030, BIG5, and other non-ASCII file detection unchanged 3. Verify normal paths where uchardet and ICU agree are unaffected fix: 编码交叉校验中优先信任 ICU 检测结果 1. 根因:selectCoding() 回退路径在 uchardet 与 ICU 结果冲突时 无条件返回 uchardet 结果,导致 UTF-8 中文 HTML 被误判为 TIS-620 2. 方案:增加交叉校验,当 uchardet 与 ICU[0] 完全不一致且 ICU[0] 为 UTF-8 时,优先返回 ICU[0] 而非 uchardet 结果 3. 影响:仅影响检测器冲突的回退路径;正常路径(包含匹配、 GB18030 优先)行为不变 Log: 修复打开 UTF-8 文件被误判为 TIS-620 导致的内容丢失 Influence: 1) 触发 TIS-620 误判的 UTF-8 HTML 打开后内容完整 2) GB18030/BIG5 文件检测与转码不变 3) 检测器一致的正常路径无回归 PMS: BUG-383047 --- src/encodes/detectcode.cpp | 9 +++++++++ tests/common2/test_detectcode.cpp | 1 + 2 files changed, 10 insertions(+) diff --git a/src/encodes/detectcode.cpp b/src/encodes/detectcode.cpp index 579099bc..17aa932f 100644 --- a/src/encodes/detectcode.cpp +++ b/src/encodes/detectcode.cpp @@ -451,6 +451,15 @@ QByteArray DetectCode::selectCoding(QByteArray ucharDetectdRet, QByteArrayList i return icuDetectRetList[0]; } qCDebug(appLog) << "Returning uchardetDetectRet as fallback"; + // uchardet 与 ICU[0] 完全无包含关系时的交叉校验: + // 若 ICU[0] 为 UTF-8 而 uchardet 结果非 UTF 变体(UTF-16/UTF-32 等), + // 优先信任 ICU,避免 UTF-8 文件被 uchardet 误判为 TIS-620 等编码 + if (icuDetectRetList[0].contains("UTF-8") && + !ucharDetectdRet.contains("UTF-16") && + !ucharDetectdRet.contains("UTF-32")) { + qCDebug(appLog) << "ICU[0] is UTF-8 and uchardet is not UTF variant, trusting ICU"; + return icuDetectRetList[0]; + } return ucharDetectdRet; } } diff --git a/tests/common2/test_detectcode.cpp b/tests/common2/test_detectcode.cpp index 94798534..014d138e 100644 --- a/tests/common2/test_detectcode.cpp +++ b/tests/common2/test_detectcode.cpp @@ -304,6 +304,7 @@ INSTANTIATE_TEST_SUITE_P( SelectCase{QByteArrayLiteral("BIG5"), {QByteArrayLiteral("GB18030")}, 0.99f, false, true, QByteArrayLiteral("GB18030")},// S4 含 GB18030 SelectCase{QByteArrayLiteral("UTF-16"), {QByteArrayLiteral("UTF-16BE")}, 0.99f, false, true, QByteArrayLiteral("UTF-16BE")}, // S5 icu[0] 包含 SelectCase{QByteArrayLiteral("EUC-JP"), {QByteArrayLiteral("SHIFT-JIS")}, 0.99f, false, true, QByteArrayLiteral("EUC-JP")}, // S6 兜底 + SelectCase{QByteArrayLiteral("TIS-620"), {QByteArrayLiteral("UTF-8")}, 0.99f, false, true, QByteArrayLiteral("UTF-8")}, // S6 交叉校验:ICU[0]=UTF-8, uchardet=TIS-620 → 信任 ICU SelectCase{QByteArrayLiteral(""), {QByteArrayLiteral("GB18030")}, 0.99f, false, true, QByteArrayLiteral("GB18030")}, // S7 空 uchar+GB SelectCase{QByteArrayLiteral(""), {QByteArrayLiteral("UTF-16LE")}, 0.99f, false, true, QByteArrayLiteral("UTF-16LE")})); // S7 空 uchar+首项