Skip to content

fix: guard null pointer in updateViewModeActions - #601

Open
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/bug-186
Open

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/bug-186

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

fix: guard null pointer in updateViewModeActions

  1. Root cause: m_actLivePreview is initialized to nullptr and only
    assigned in initRightClickedMenu(), but updateViewModeActions() can
    be called via signal/slot before initialization completes
  2. Fix: add null check before calling setEnabled on m_actLivePreview,
    consistent with the existing null-safe pattern on the preceding line
  3. Impact: prevents null pointer dereference crash when switching view
    modes before right-click menu initialization

Influence:

  1. Test switching view modes before opening right-click menu
  2. Test markdown file open and view mode toggle
  3. Verify no regression on normal view mode switching

fix: 修复updateViewModeActions空指针解引用问题

  1. 根因:m_actLivePreview初始化为nullptr,仅在initRightClickedMenu()
    中赋值,但updateViewModeActions()可通过signal/slot在初始化前被调用
  2. 方案:在调用m_actLivePreview->setEnabled前添加空指针检查,与同
    函数已有的空安全模式保持一致
  3. 影响:防止右键菜单初始化前切换视图模式导致的空指针崩溃

Influence:

  1. 测试右键菜单初始化前切换视图模式
  2. 测试Markdown文件打开及视图模式切换
  3. 验证正常视图模式切换无回归

PMS: BUG-186

Summary by Sourcery

Bug Fixes:

  • Prevent a null-pointer crash when updating view-mode actions before the live-preview action is initialized.

1. Root cause: m_actLivePreview is initialized to nullptr and only
   assigned in initRightClickedMenu(), but updateViewModeActions() can
   be called via signal/slot before initialization completes
2. Fix: add null check before calling setEnabled on m_actLivePreview,
   consistent with the existing null-safe pattern on the preceding line
3. Impact: prevents null pointer dereference crash when switching view
   modes before right-click menu initialization

Influence:
1. Test switching view modes before opening right-click menu
2. Test markdown file open and view mode toggle
3. Verify no regression on normal view mode switching

fix: 修复updateViewModeActions空指针解引用问题

1. 根因:m_actLivePreview初始化为nullptr,仅在initRightClickedMenu()
   中赋值,但updateViewModeActions()可通过signal/slot在初始化前被调用
2. 方案:在调用m_actLivePreview->setEnabled前添加空指针检查,与同
   函数已有的空安全模式保持一致
3. 影响:防止右键菜单初始化前切换视图模式导致的空指针崩溃

Influence:
1. 测试右键菜单初始化前切换视图模式
2. 测试Markdown文件打开及视图模式切换
3. 验证正常视图模式切换无回归

PMS: BUG-186

@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 @pengfeixx, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 3 days and 13 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pengfeixx

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

@sourcery-ai

sourcery-ai Bot commented Sep 18, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Prevents a null-pointer dereference when view-mode signals invoke updateViewModeActions before the right-click menu initializes the live-preview action, while retaining normal Markdown and view-mode behavior.

Sequence diagram for guarded view mode action updates

sequenceDiagram
    participant ViewModeSignal
    participant TextEdit
    participant LivePreviewAction
    ViewModeSignal->>TextEdit: updateViewModeActions(mode, isMarkdown)
    TextEdit->>TextEdit: action->isChecked()
    TextEdit->>TextEdit: action->setChecked(true)
    alt m_actLivePreview initialized
        TextEdit->>LivePreviewAction: setEnabled(isMarkdown)
    else m_actLivePreview is null
        TextEdit-->>TextEdit: skip setEnabled
    end
Loading

File-Level Changes

Change Details Files
Guard live-preview action access during view-mode updates.
  • Check the action pointer before enabling or disabling it.
  • Preserve existing Markdown-specific enablement behavior when the action is initialized.
src/editor/dtextedit.cpp

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 100 分,大于 70 分通过阈值,代码质量符合要求。本次提交修复了 updateViewModeActions 函数中的空指针解引用问题,修改最小化且与同函数已有的空安全模式一致。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

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

建议: 语法正确,逻辑清晰。在调用 m_actLivePreview->setEnabled(isMarkdown) 前添加空指针检查,与同函数前一行 if (action && !action->isChecked()) 的空安全模式保持一致,修复了 m_actLivePreview 在 initRightClickedMenu() 赋值前通过 signal/slot 触发 updateViewModeActions() 导致的空指针解引用崩溃。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

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

建议: 代码结构清晰,注释完整。修改仅涉及2行代码(1行删除、2行新增),变更最小化且聚焦。保留了原有注释「置灰规则(§4.4):非 md 文件仅「实时预览」置灰,编辑/查看始终可用」,代码风格与上下文完全一致。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

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

建议: 性能良好,资源使用合理。新增的空指针判断(if (m_actLivePreview))为常数时间操作,开销可忽略不计,对性能无任何影响。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

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

建议: 存在0个安全漏洞。本次修改实际上是安全加固,防止了空指针解引用导致的程序崩溃。无需额外安全加固。


💡 改进建议代码示例

// 修复后的代码(src/editor/dtextedit.cpp,updateViewModeActions 函数)
void TextEdit::updateViewModeActions(ViewMode mode, bool isMarkdown)
{
    // ... 前置逻辑 ...
    if (action && !action->isChecked())
        action->setChecked(true);
    // 置灰规则(§4.4):非 md 文件仅「实时预览」置灰,编辑/查看始终可用
    if (m_actLivePreview)  // 新增:空指针检查
        m_actLivePreview->setEnabled(isMarkdown);
}

// 修复说明:
// m_actLivePreview 初始化为 nullptr,仅在 initRightClickedMenu() 中赋值。
// updateViewModeActions() 可通过 signal/slot 在初始化前被调用,
// 添加空指针检查防止崩溃,与同函数已有的空安全模式一致。

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

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.

2 participants