Skip to content

fix: remap GB18030-2022 PUA codepoints to equivalent Unicode - #735

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix/bug-382849-gb18030-pua
Nov 17, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix/bug-382849-gb18030-pua

Conversation

@add-uos

@add-uos add-uos commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

fix: 修复 GB18030-2022 特殊字符在编辑器中显示乱码

PMS: BUG-382849(【V25 2500U2】【文本编辑器】【6.5.65】打开GB18030编码格式的文件显示乱码)

1. 根因

GB18030-2022 标准系统上(glibc ≥ 2.38),iconv 将以下 GB18030 码位转换为 PUA 私用区码位,而 PUA 无标准字形覆盖,导致编辑器显示乱码(豆腐块或字体 fallback 出怪字):

GB18030 码位 glibc iconv 输出 等价正式 Unicode
FE51 FE52 FE53 FE6C FE76 FE91(双字节) U+E816 E817 E818 E831 E83B E855 U+20087 20089 200CC 215D7 2298F 241FE
8431 8236 ~ 8431 8335(四字节) U+E78D ~ U+E796 U+FE10 ~ U+FE19(竖排形式)
8235 9037 ~ 8235 9134(四字节) U+E81E E826 E82B E82C E832 E843 E854 E864 U+9FB4 ~ U+9FBB(CJK 扩展 B)

等价关系的依据:

  • glibc localedata/charmaps/GB18030(commit e1d3312015, BZ #30243)官方注释明确标注:These PUA mappings use equivalents <U20087>, <U20089> and <U200CC>、This 1 PUA mapping uses the equivalent <U9FB4>
  • 同一字符的另一种 GB18030 编码在 glibc 下直接解码为正式码位(如双字节 A6D9 → U+FE10,四字节 8431 8236 → U+E78D,两者为同一字符)
  • Python 3.12 的 gb18030 codec(2022 表)对四字节码位直接映射正式码位
  • 微软/WHATWG GB18030 四字节 ranges 表同样直接映射正式码位

2. 方案

在 iconv 转换后应用 PUA → 正式 Unicode 重映射表(共 24 项):

  • 竖排形式:E78DE796 → U+FE10FE19
  • CJK 扩展 B:E81E 系列 → U+9FB4~9FBB;E816 系列 → U+20087 等
  • 同时将 toMib 设置与 GB18030 替换逻辑移出 enablePatchedIconv 门控,使标准 iconv 环境下同样生效

3. 验证

  • 单元测试:test_common2 149/149 通过(含新增 E78D→FE10、E81E→9FB4 端到端测试)
  • 实测 GB18030-2022 测试文件(每行 = 同一字符的双字节+四字节编码):重映射后剪贴板读出 54 字符、PUA 字符 18 → 0,每行正常显示竖排标点与扩展 B 汉字
  • 目标码位 U+FE10FE19 / U+9FB49FBB / U+20087 系在本机有 56~60 个字体覆盖

4. 影响

  • 含 FE51 系、四字节 8431xxxx/8235xxxx 特殊字符的 GB18030 文件在 2022 标准系统上可正常显示
  • 2005 标准系统无行为变化

Log: 修复 GB18030-2022 特殊字符显示乱码的问题

Summary by Sourcery

Remap GB18030-2022 iconv private-use output to equivalent Unicode code points so affected characters render correctly in the editor.

Bug Fixes:

  • Prevent GB18030-2022 characters from displaying as unsupported glyphs by remapping iconv-produced private-use code points to their equivalent standard Unicode characters.

Enhancements:

  • Apply GB18030 conversion compatibility handling consistently, including environments that use standard iconv without the patched-iconv option.

Tests:

  • Add coverage for remapping GB18030-2022 vertical punctuation and CJK characters under standard iconv.

@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Define the GB18030-2022 PUA-to-Unicode mappings and apply them to decoded GB18030 data after iconv conversion.

  • src/encodes/detectcode.cpp L103-134, L974-984

Detect the destination encoding and apply GB18030 replacement handling regardless of whether patched iconv is enabled.

  • src/encodes/detectcode.cpp L846 (removed lines), L848-863

Tests

Update the expected remapped output and add standard-iconv coverage for vertical punctuation and CJK Extension B mappings.

  • tests/common2/test_detectcode.cpp L811-817, L844-869 (+2 small edits)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/encodes/detectcode.cpp" line_range="980" />
<code_context>
+        // 无字体覆盖导致乱码,重映射为 2005 标准 Unicode 码位
+        if (GB18030 == fromMib && UTF_8 == toMib) {
+            for (auto itr = gs_PUA2022ToUnicode2005.begin(); itr != gs_PUA2022ToUnicode2005.end(); ++itr) {
+                convertedData.replace(itr.key(), itr.value());
+            }
+        }
</code_context>
<issue_to_address>
**Valid private-use text changes**

When GB18030 decoding produces a mapped PUA scalar from intentional private-use text or a valid legacy mapping, `convertedData.replace()` changes every matching PUA scalar, including ones that should be preserved; `readFile` returns different text and saving can persist the corruption.

Apply the remapping only to the GB18030 byte sequences whose interpretation changed, rather than to every matching decoded PUA scalar.
</issue_to_address>

### Comment 2
<location path="src/encodes/detectcode.cpp" line_range="858" />
<code_context>
-            if (GB18030 == fromMib && UTF_8 == toMib) {
-                qCDebug(appLog) << "Converting from GB18030 to UTF-8, applying 2005 error replacements";
-                for (auto itr = gs_ReplaceFromGB18030_2005Error.begin(); itr != gs_ReplaceFromGB18030_2005Error.end(); ++itr) {
-                    inputStr.replace(itr.key(), itr.value());
-                }
-            } else if (UTF_8 == fromMib && GB18030 == toMib) {
</code_context>
<issue_to_address>
**Neighboring characters are corrupted**

When a valid two-byte GB18030 character ending in `0xFE` is followed by ASCII `Q` (`0x51`), `inputStr.replace()` matches `FE51` across the character boundary and inserts the sentinel; the error handler then emits incorrect text for the neighboring characters.

Apply replacements only to complete GB18030 character sequences, without matching across character boundaries.
</issue_to_address>

### Comment 3
<location path="src/encodes/detectcode.cpp" line_range="861-864" />
<code_context>
-                    inputStr.replace(itr.key(), itr.value());
-                }
-            } else if (UTF_8 == fromMib && GB18030 == toMib) {
-                qCDebug(appLog) << "Converting from UTF-8 to GB18030, applying 2020 error replacements";
-                for (auto itr = gs_ReplaceToGB18030_2020Error.begin(); itr != gs_ReplaceToGB18030_2020Error.end(); ++itr) {
-                    inputStr.replace(itr.key(), itr.value());
-                }
+        // 替换需手动修改 UTF-8 和 GB18030 编码转换的部分
+        if (GB18030 == fromMib && UTF_8 == toMib) {
</code_context>
<issue_to_address>
**GB18030 saves corrupt Unicode**

When standard iconv is used to save UTF-8 text containing one of the six mapped Unicode characters as GB18030, `ChangeFileEncodingFormat` replaces characters such as U+20087 with the `FFFF11` sentinel, but `checkUTF8ToGB18030Error` does not recognize that sentinel; its EILSEQ path emits `?` while consuming the sentinel bytes, so saving these characters as GB18030 corrupts them.

Handle the sentinel explicitly in the EILSEQ path, or avoid substituting it on iconv paths that cannot decode it.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

// 无字体覆盖导致乱码,重映射为 2005 标准 Unicode 码位
if (GB18030 == fromMib && UTF_8 == toMib) {
for (auto itr = gs_PUA2022ToUnicode2005.begin(); itr != gs_PUA2022ToUnicode2005.end(); ++itr) {
convertedData.replace(itr.key(), itr.value());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Valid private-use text changes

When GB18030 decoding produces a mapped PUA scalar from intentional private-use text or a valid legacy mapping, convertedData.replace() changes every matching PUA scalar, including ones that should be preserved; readFile returns different text and saving can persist the corruption.

Apply the remapping only to the GB18030 byte sequences whose interpretation changed, rather than to every matching decoded PUA scalar.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 980:

**Valid private-use text changes**

When GB18030 decoding produces a mapped PUA scalar from intentional private-use text or a valid legacy mapping, `convertedData.replace()` changes every matching PUA scalar, including ones that should be preserved; `readFile` returns different text and saving can persist the corruption.

Apply the remapping only to the GB18030 byte sequences whose interpretation changed, rather than to every matching decoded PUA scalar.

if (GB18030 == fromMib && UTF_8 == toMib) {
qCDebug(appLog) << "Converting from GB18030 to UTF-8, applying 2005 error replacements";
for (auto itr = gs_ReplaceFromGB18030_2005Error.begin(); itr != gs_ReplaceFromGB18030_2005Error.end(); ++itr) {
inputStr.replace(itr.key(), itr.value());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Neighboring characters are corrupted

When a valid two-byte GB18030 character ending in 0xFE is followed by ASCII Q (0x51), inputStr.replace() matches FE51 across the character boundary and inserts the sentinel; the error handler then emits incorrect text for the neighboring characters.

Apply replacements only to complete GB18030 character sequences, without matching across character boundaries.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 858:

**Neighboring characters are corrupted**

When a valid two-byte GB18030 character ending in `0xFE` is followed by ASCII `Q` (`0x51`), `inputStr.replace()` matches `FE51` across the character boundary and inserts the sentinel; the error handler then emits incorrect text for the neighboring characters.

Apply replacements only to complete GB18030 character sequences, without matching across character boundaries.

Comment on lines +861 to 864
qCDebug(appLog) << "Converting from UTF-8 to GB18030, applying 2020 error replacements";
for (auto itr = gs_ReplaceToGB18030_2020Error.begin(); itr != gs_ReplaceToGB18030_2020Error.end(); ++itr) {
inputStr.replace(itr.key(), itr.value());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · GB18030 saves corrupt Unicode

When standard iconv is used to save UTF-8 text containing one of the six mapped Unicode characters as GB18030, ChangeFileEncodingFormat replaces characters such as U+20087 with the FFFF11 sentinel, but checkUTF8ToGB18030Error does not recognize that sentinel; its EILSEQ path emits ? while consuming the sentinel bytes, so saving these characters as GB18030 corrupts them.

Handle the sentinel explicitly in the EILSEQ path, or avoid substituting it on iconv paths that cannot decode it.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at lines 861-864:

**GB18030 saves corrupt Unicode**

When standard iconv is used to save UTF-8 text containing one of the six mapped Unicode characters as GB18030, `ChangeFileEncodingFormat` replaces characters such as U+20087 with the `FFFF11` sentinel, but `checkUTF8ToGB18030Error` does not recognize that sentinel; its EILSEQ path emits `?` while consuming the sentinel bytes, so saving these characters as GB18030 corrupts them.

Handle the sentinel explicitly in the EILSEQ path, or avoid substituting it on iconv paths that cannot decode it.

Root cause: on GB18030-2022 systems glibc iconv converts FE51 and
four-byte sequences 8431xxxx/8235xxxx to PUA codepoints (U+E816,
U+E78D, U+E81E etc.) which have no standard font glyph coverage,
causing garbled display in the editor.

Fix: extend the post-iconv PUA remapping table to 24 entries,
covering vertical forms (E78D-E796 -> U+FE10-FE19), CJK Ext B
(E81E series -> U+9FB4-9FBB, E816 series -> U+20087 etc.), and
move the GB18030 replacement logic outside the enablePatchedIconv
gate so it also runs with standard iconv.

在 GB18030-2022 标准系统上,glibc iconv 将 FE51 及四字节序列
8431xxxx/8235xxxx 转换为无标准字形覆盖的 PUA 码位(U+E816、
U+E78D、U+E81E 等),导致编辑器显示乱码。

扩展 iconv 转换后的 PUA 重映射表至 24 项:竖排形式
(E78D-E796 -> U+FE10-FE19)、CJK 扩展 B(E81E 系列 ->
U+9FB4-9FBB、E816 系列 -> U+20087 等),并将 GB18030 替换
逻辑移出 enablePatchedIconv 门控,使标准 iconv 下同样生效。

Log: 修复 GB18030-2022 特殊字符显示乱码问题
PMS: BUG-382849
@add-uos
add-uos force-pushed the fix/bug-382849-gb18030-pua branch from a7260b7 to ba278db Compare November 17, 2026 09:29
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交修复了 GB18030-2022 标准下 iconv 将部分码位映射到 PUA 区域(无字体覆盖导致乱码)的问题, 新增 post-iconv PUA 重映射表将 PUA 码位转换为有字体覆盖的 Unicode 2005 标准码位, 同时移除了 enablePatchedIconv 条件门控使编码替换逻辑始终生效。代码逻辑正确、注释完整、 测试覆盖充分,四维度均通过,未发现安全漏洞。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

本次提交无需修复,代码质量良好。建议保持现有注释风格,后续如需扩展映射表可考虑按 Unicode 区段 分组并添加区段头注释。

本报告由 AI 代码审查工具自动生成

@add-uos

add-uos commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 4786dd6 into linuxdeepin:master Nov 17, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants