Skip to content

fix: fix null pointer dereference in removeAllAnnotation guard condition - #395

Closed
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/pms-bug-bot/dadc2e1ceac8
Closed

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/pms-bug-bot/dadc2e1ceac8

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

fix: fix null pointer dereference in removeAllAnnotation guard condition

The guard condition in removeAllAnnotation() had a boolean logic flaw
where the null check was embedded inside the contents check as
(annota && annota->contents().isEmpty()). When annota is null, this
subexpression short-circuits to false, and !m_annotations.contains(annota)
also evaluates to false when the list contains the null pointer. This
causes the null pointer to bypass the guard and be dereferenced at
annota->boundary().

Fix by reordering the condition to check !annota first, short-circuiting
all null pointers before any dereference.

Fixes: #205

Summary by Sourcery

Bug Fixes:

  • Prevent null annotation pointers from being dereferenced while removing annotations.

The guard condition in removeAllAnnotation() had a boolean logic flaw
where the null check was embedded inside the contents check as
`(annota && annota->contents().isEmpty())`. When annota is null, this
subexpression short-circuits to false, and `!m_annotations.contains(annota)`
also evaluates to false when the list contains the null pointer. This
causes the null pointer to bypass the guard and be dereferenced at
`annota->boundary()`.

Fix by reordering the condition to check `!annota` first, short-circuiting
all null pointers before any dereference.

Fixes: linuxdeepin#205

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

Fixes a null pointer dereference in removeAllAnnotation() by checking for null annotations first, ensuring invalid pointers are skipped before any method calls.

Flow diagram for null-safe annotation removal

flowchart TD
    A["removeAllAnnotation() iterates annotations"] --> B{"annota is null?"}
    B -- Yes --> C["continue"]
    B -- No --> D{"annotation is absent or contents are empty?"}
    D -- Yes --> C
    D -- No --> E["annota->boundary()"]
Loading

File-Level Changes

Change Details Files
Reordered the remove-all-annotations guard to reject null annotation pointers before evaluating list membership or dereferencing annotation methods.
  • Added an early !annota short-circuit condition.
  • Preserved existing membership and empty-content checks for non-null annotations.
  • Prevented null pointers from reaching the boundary collection logic.
reader/browser/BrowserPage.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 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 99 分,大于 70 分通过阈值。本次提交修复了 removeAllAnnotation 函数中的空指针解引用缺陷,修改精准且逻辑正确,未引入新的安全漏洞或性能问题。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

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

建议: 修复正确,将空指针检查 !annota 置于短路 OR 链首位,确保 annota 为 null 时立即 continue,避免后续对 annota->contents() 和 annota->boundary() 的空指针解引用。原代码逻辑缺陷已修复。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

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

建议: 修复精准且最小化。建议后续可考虑移除冗余的 contains 检查以简化条件逻辑。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

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

建议: 新增一个短路布尔检查,性能影响可忽略不计。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

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

建议: 存在0个安全漏洞。修复提升了代码健壮性,防止空指针解引用导致的潜在崩溃。


💡 改进建议代码示例

// 修复后的代码(当前提交已包含此修复)
// reader/browser/BrowserPage.cpp - removeAllAnnotation() 函数
bool BrowserPage::removeAllAnnotation()
{
    for (int index = 0; index < m_annotations.size(); index++) {
        deepin_reader::Annotation *annota = m_annotations.at(index);

        // 将空指针检查置于首位,利用短路求值确保安全
        if (!annota || !m_annotations.contains(annota) || annota->contents().isEmpty())
            continue;

        annoBoundaries << annota->boundary();
        // ...
    }
}

// 原始问题代码(已修复):
// if (!m_annotations.contains(annota) || (annota && annota->contents().isEmpty()))
// 当 annota 为 null 时,contains 可能返回 true,导致条件不满足,
// 继续执行 annota->boundary() 造成空指针解引用崩溃。

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

@pengfeixx

Copy link
Copy Markdown
Contributor Author

Superseded by #400, which combines this fix with #397 into a single commit (same content).

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