Skip to content

feat: add safe preview and controlled save for files containing NUL bytes#489

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:feat/invalid-char-preview
Jul 23, 2026
Merged

feat: add safe preview and controlled save for files containing NUL bytes#489
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:feat/invalid-char-preview

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented Jul 22, 2026

Copy link
Copy Markdown
Member

概述

为 deepin-editor 实现"含无效字符 NUL 文件的安全预览与受控保存"功能。

当文件包含 \x00 NUL 字节时,编辑器进入只读预览模式,将 NUL 显示为可见文本 \00,并通过明确确认流程控制编辑和保存,避免用户误覆盖原文件造成不可逆损坏。

主要改动

模块 改动
FileLoadThread 在原始字节层面检测 NUL,转义为 \00
EditWrapper 预览态字段、只读切换、Edit Anyway、保存成功后退出预览
WarningNotices 新增 "Edit Anyway" 按钮与信号
Window 统一 confirmInvalidCharSave 三按钮确认弹窗(Don't Save / Save As / Save Anyway),Ctrl+S 与关闭 Tab 共用同一入口;预览模式拒绝同路径 Save As;备份短路
CSyntaxHighlighter \00 高亮(红色背景 + 白色文字)
翻译文件 新增 en / zh_CN / zh_HK / zh_TW 文案

验收标准

  • US-1 打开含 NUL 文件:显示 hello\00world,默认只读,警告条 + Edit Anyway,\00 高亮,原文件不变
  • US-2 Edit Anyway:可编辑,警告条关闭,仍处预览模式
  • US-3 Ctrl+S:三按钮确认框,Don't Save / Save As / Save Anyway 各路径正确
  • US-4 关闭 Tab:复用同一弹窗,取消不关闭
  • US-5 Save As 保护:拒绝同路径,新路径成功,原文件保留 NUL
  • US-6 会话恢复:预览态修改不保存退出后,重新检测原文件进入只读预览,不恢复未保存编辑稿
  • US-7 普通 Save As 回归:无异常 *

测试

已通过人工功能验证(US-1 至 US-7)及 AI 代码审查。

@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.

Sorry @dengzhongyuan365-dev, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

…ng NUL bytes

When a file contains \x00 NUL bytes, the editor now enters a read-only
preview mode that displays NUL as visible text \00 with red highlight,
shows a warning bar with an "Edit Anyway" button, and enforces a
three-button confirmation dialog (Don't Save / Save As / Save Anyway)
for both Ctrl+S and close-tab flows. Save As to the original path is
rejected in preview mode. Auto-backup is short-circuited during preview
to prevent persisting unsaved preview edits across sessions.

Key changes:
- FileLoadThread: detect NUL bytes at raw byte level, escape to \00
- EditWrapper: preview state fields, read-only toggle, Edit Anyway,
exit-preview on successful save
- WarningNotices: add "Edit Anyway" button and signal
- Window: unified confirmInvalidCharSave dialog, Save As same-path
rejection, backup short-circuit for preview mode
- CSyntaxHighlighter: \00 highlight (red background, white text)
- Translations: new messages for en, zh_CN, zh_HK, zh_TW

PMS: ztgd2026062500034
@dengzhongyuan365-dev
dengzhongyuan365-dev force-pushed the feat/invalid-char-preview branch from 4a93eb5 to b032397 Compare July 23, 2026 01:07
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:92分

■ 【总体评价】

代码实现了对包含 NUL 字节文件的只读预览与安全保存机制,整体逻辑严谨且用户体验良好
逻辑正确且无安全漏洞,仅在文件读取转义时存在轻微性能损耗

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

Window::closeTab 中重构了 isModified 的判断逻辑,预览模式分支内所有路径均正确返回,未破坏原有非预览模式的保存提示流程。EditWrapper::handleFileLoadFinished 中针对 hasNul 的状态初始化与 UI 交互逻辑完整。
潜在问题:EditWrapper::handleFileLoadFinished 中动态创建 CSyntaxHighlighter 时,若 Settings::instance()->settings 链中存在空指针可能引发崩溃;Window::closeTabisModified 变量的上层定义未在 diff 中展示,若未正确初始化可能导致逻辑异常。
建议:对 Settings::instance()->settings->option 的返回值进行空指针校验;确保 isModified 在进入 else 块前已通过如 wrapper->isModified() 正确赋值。

  • 2.代码质量(良好)✓

代码结构清晰,新增的预览模式状态变量命名规范(如 m_bInvalidCharPreview),注释详尽地解释了预览模式拦截和跳过备份的原因。翻译文件同步更新完整。
潜在问题:FileLoadThread::run 中对 NUL 字符的检测与转义逻辑在预处理和最终处理中存在重复代码。
建议:可将 NUL 检测与转义逻辑封装为独立的工具函数,减少代码冗余。

  • 3.代码性能(良好)✓

CSyntaxHighlighter::highlightBlock 中使用静态正则表达式对象避免了重复编译开销,处理逻辑高效。
潜在问题:FileLoadThread::run 中对 indata 先调用 contains 再调用 replace,对大文件会导致两次遍历。
建议:可使用 indexOf 或直接在 replace 后通过返回值判断是否发生替换,以减少一次全量遍历。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码未引入外部输入导致的注入风险,文件路径比较使用了 QFileInfo::absoluteFilePath 规范化路径,避免了路径遍历绕过。强制保存操作由用户二次确认,不构成安全漏洞。

  • 建议:保持现有安全校验机制,持续关注文件保存时的权限处理。

■ 【改进建议代码示例】

// src/common/fileloadthread.cpp
// 优化 NUL 字符检测与转义的性能,减少一次全量遍历
bool hasNul = false;
int nulIndex = indata.indexOf('\x00');
if (nulIndex != -1) {
    hasNul = true;
    qDebug() << "NUL bytes detected in file, escaping to \\00";
    indata.replace('\x00', "\\00");
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, 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

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/merge

@deepin-bot

deepin-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

This pr cannot be merged! (status: unstable)

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit b5df59d into linuxdeepin:master Jul 23, 2026
20 of 21 checks passed
dengzhongyuan365-dev added a commit to dengzhongyuan365-dev/deepin-editor that referenced this pull request Jul 23, 2026
…ytes

Cherry-pick of PR linuxdeepin#489 (merge commit b5df59d) from master to
release/eagle. Conflicts resolved preferring release/eagle base while
preserving all NUL safety preview functionality.

When a file contains \x00 NUL bytes, the editor enters a read-only
preview mode that displays NUL as visible text \00 with red highlight,
shows a warning bar with an "Edit Anyway" button, and enforces a
three-button confirmation dialog (Don't Save / Save As / Save Anyway)
for both Ctrl+S and close-tab flows. Save As to the original path is
rejected in preview mode. Auto-backup is short-circuited during preview
to prevent persisting unsaved preview edits across sessions.

PMS: bug-371363
Multica Issue: https://agent-dev.uniontech.com/issues/14dc6e9a-d392-4a6a-b8a9-f295ea204c96
dengzhongyuan365-dev added a commit to dengzhongyuan365-dev/deepin-editor that referenced this pull request Jul 23, 2026
…ytes

Cherry-pick of PR linuxdeepin#489 (merge commit b5df59d) from master to
release/eagle. Conflicts resolved preferring release/eagle base while
preserving all NUL safety preview functionality.

When a file contains \x00 NUL bytes, the editor enters a read-only
preview mode that displays NUL as visible text \00 with red highlight,
shows a warning bar with an "Edit Anyway" button, and enforces a
three-button confirmation dialog (Don't Save / Save As / Save Anyway)
for both Ctrl+S and close-tab flows. Save As to the original path is
rejected in preview mode. Auto-backup is short-circuited during preview
to prevent persisting unsaved preview edits across sessions.

PMS: bug-371363
Multica Issue: https://agent-dev.uniontech.com/issues/14dc6e9a-d392-4a6a-b8a9-f295ea204c96
dengzhongyuan365-dev added a commit to dengzhongyuan365-dev/deepin-editor that referenced this pull request Jul 24, 2026
…ytes

Cherry-pick of PR linuxdeepin#489 (merge commit b5df59d) from master to
release/eagle. Conflicts resolved preferring release/eagle base while
preserving all NUL safety preview functionality.

When a file contains \x00 NUL bytes, the editor enters a read-only
preview mode that displays NUL as visible text \00 with red highlight,
shows a warning bar with an "Edit Anyway" button, and enforces a
three-button confirmation dialog (Don't Save / Save As / Save Anyway)
for both Ctrl+S and close-tab flows. Save As to the original path is
rejected in preview mode. Auto-backup is short-circuited during preview
to prevent persisting unsaved preview edits across sessions.

PMS: bug-371363
Multica Issue: https://agent-dev.uniontech.com/issues/14dc6e9a-d392-4a6a-b8a9-f295ea204c96
deepin-bot Bot pushed a commit that referenced this pull request Jul 24, 2026
…ytes

Cherry-pick of PR #489 (merge commit b5df59d) from master to
release/eagle. Conflicts resolved preferring release/eagle base while
preserving all NUL safety preview functionality.

When a file contains \x00 NUL bytes, the editor enters a read-only
preview mode that displays NUL as visible text \00 with red highlight,
shows a warning bar with an "Edit Anyway" button, and enforces a
three-button confirmation dialog (Don't Save / Save As / Save Anyway)
for both Ctrl+S and close-tab flows. Save As to the original path is
rejected in preview mode. Auto-backup is short-circuited during preview
to prevent persisting unsaved preview edits across sessions.

PMS: bug-371363
Multica Issue: https://agent-dev.uniontech.com/issues/14dc6e9a-d392-4a6a-b8a9-f295ea204c96
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