Skip to content

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

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
dengzhongyuan365-dev:feat/invalid-char-preview-eagle
Jul 24, 2026
Merged

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

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member

概述

将 PR #489(已合入 master)的 NUL 安全预览与受控保存功能同步到 release/eagle 分支。

同步说明

  • Base: release/eagle HEAD fa2f750
  • 冲突解决策略:以 release/eagle 现状为准,保留 NUL 安全预览全部功能
  • release/eagle 与 master 的差异(如 m_encodeHint、debug 日志、setEndlineFormat API 等)已按 release/eagle 既有风格适配,不影响功能语义

主要改动

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

关联

@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

@dengzhongyuan365-dev
dengzhongyuan365-dev force-pushed the feat/invalid-char-preview-eagle branch 2 times, most recently from 204f59d to a2b92dd Compare July 23, 2026 10:45
…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
dengzhongyuan365-dev force-pushed the feat/invalid-char-preview-eagle branch from a2b92dd to 234a574 Compare July 24, 2026 03:30
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码实现了对包含NUL字符文件的安全预览与编辑控制,逻辑严谨且交互完善
整体质量良好,状态管理清晰,安全防护到位,仅在代码复用上有微小优化空间

■ 【详细分析】

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

FileLoadThread::run 中对 \x00 的检测与替换逻辑正确,QByteArray::replace 能有效处理所有 NUL 字节。CSyntaxHighlighter::highlightBlock 中使用 static const QRegularExpression 匹配 \\00 字面量,正则表达式转义正确。Window::closeTabWindow::saveFile 中的状态判断与弹窗逻辑分支完整,覆盖了不保存、另存为、强制保存三种情况。
潜在问题:FileLoadThread::runsigPreProcesssigLoadFinished\x00 的替换逻辑存在重复代码,若未来需调整转义符格式需两处同步修改。
建议:将 NUL 替换逻辑抽取为独立的内联函数或静态方法,减少代码冗余。

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

新增的成员变量命名规范,如 m_bInvalidCharPreviewm_bInvalidCharEditAllowed 表意清晰。注释详尽,特别是在 OnUpdateHighlighter 中解释了为何在预览模式下保持高亮开启的原因。翻译文件同步更新,多语言支持完善。
潜在问题:无
建议:保持现有的注释风格,有助于后续维护。

  • 3.代码性能(无性能问题)✓

QByteArray::replace 虽然在处理大文件时会有内存拷贝开销,但为保障数据完整性是必要的。highlightBlock 中的正则匹配使用了 static 变量,避免了重复编译正则表达式的开销。预览模式下保持高亮开启虽然会增加重绘开销,但符合功能需求且影响可控。
潜在问题:无
建议:无

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码在处理文件路径和保存逻辑时进行了严格校验。Window::saveAsFileToDisk 中通过 QFileInfo(originalPath).absoluteFilePath() == QFileInfo(newFilePath).absoluteFilePath() 防止了在预览模式下覆盖原文件,避免了数据损坏风险。强制保存和另存为操作均需用户明确确认,无越权或静默操作风险。

  • 建议:无

■ 【改进建议代码示例】

// src/common/fileloadthread.cpp
// 建议抽取公共替换逻辑
inline QByteArray replaceNulBytes(const QByteArray &data) {
    if (data.contains('\x00')) {
        QByteArray replacedData = data;
        replacedData.replace('\x00', "\\00");
        return replacedData;
    }
    return data;
}

void FileLoadThread::run()
{
    // ... 前置逻辑
            QString textEncode = QString::fromLocal8Bit(encode);
            if (textEncode.contains("ASCII", Qt::CaseInsensitive) || textEncode.contains("UTF-8", Qt::CaseInsensitive)) {
                emit sigPreProcess(encode, replaceNulBytes(indata));
            } else {
                QByteArray outHeadData;
                DetectCode::ChangeFileEncodingFormat(indata, outHeadData, textEncode, QString("UTF-8"));
                emit sigPreProcess(encode, replaceNulBytes(outHeadData));
            }
    // ... 后续逻辑
}

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

Auto-dispatch review request from Multica Issue V-310.

This PR syncs the NUL safe-preview feature (already merged to master via #489) to release/eagle, plus a highlight-timing fix verified on v20.

Reviewers requested:

Context:

Thanks!

@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

/forcemerge

@deepin-bot

deepin-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit 9735678 into linuxdeepin:release/eagle Jul 24, 2026
20 of 21 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