Skip to content

fix: crash when click close on preview window in some cases#1241

Merged
BLumia merged 1 commit into
linuxdeepin:masterfrom
BLumia:close-crash
Sep 5, 2025
Merged

fix: crash when click close on preview window in some cases#1241
BLumia merged 1 commit into
linuxdeepin:masterfrom
BLumia:close-crash

Conversation

@BLumia

@BLumia BLumia commented Sep 5, 2025

Copy link
Copy Markdown
Member

修正在存在多个预览窗口时,点击中间的某个窗口的关闭按钮时,预览可能崩溃的问题.

Summary by Sourcery

Prevent crashes when closing preview windows in multi-window scenarios by tightening index bounds checks and switching to at() for data retrieval.

Bug Fixes:

  • Prevent out-of-range access to m_data when retrieving items by replacing m_data.value with m_data.at
  • Fix incorrect bounds check in data() by changing the condition from '>' to '>=' to avoid invalid index access

修正在存在多个预览窗口时,点击中间的某个窗口的关闭按钮时,预览可能崩溃
的问题.

Log:
@BLumia BLumia requested a review from 18202781743 September 5, 2025 03:42
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

这段代码改动主要涉及对容器访问方式的优化,我来分析一下改进点和潜在问题:

  1. m_data.value() 改为 m_data.at() 的改进:
  • 原代码使用 value() 方法,当索引越界时会返回默认值
  • 新代码使用 at() 方法,当索引越界时会抛出 std::out_of_range 异常
  • 这是一个更好的实践,因为静默返回默认值可能会掩盖程序错误
  1. 边界检查条件的改进:
  • 原代码使用 > 比较,可能导致访问越界
  • 新代码使用 >= 比较,确保不会访问越界
  • 这是一个正确的边界检查方式
  1. 潜在问题和改进建议:

a) 异常处理:

  • 虽然使用 at() 更安全,但代码中没有处理可能的异常
  • 建议:在调用 at() 之前先进行边界检查,或者添加 try-catch 块处理可能的异常

b) 性能考虑:

  • at() 方法在越界时会抛出异常,异常处理有一定开销
  • 对于频繁调用的函数,建议先进行显式边界检查,避免异常

c) 代码一致性:

  • 建议在整个类中统一使用相同的容器访问方式
  • 如果选择使用 at(),建议所有类似的地方都进行相应修改

d) 防御性编程:

  • 即使使用了 at(),建议保留边界检查,这样可以更早发现问题
  • 可以考虑添加断言(assert)来帮助调试

建议的改进代码:

QVariant DockGlobalElementModel::data(const QModelIndex &index, int role) const
{
    // 显式边界检查
    if (index.row() < 0 || index.row() >= m_data.size()) {
        return {};
    }

    try {
        auto data = m_data.at(index.row());
        auto id = std::get<0>(data);
        auto model = std::get<1>(data);
        auto row = std::get<2>(data);
        // ... 其余代码
    } catch (const std::out_of_range& e) {
        qWarning() << "Out of range access in data:" << e.what();
        return {};
    }
}

这样的改进既保证了安全性,又提供了更好的错误处理机制,同时保持了代码的健壮性。

@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 there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@sourcery-ai

sourcery-ai Bot commented Sep 5, 2025

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

Reviewer's Guide

This PR prevents crashes when closing preview windows by tightening index boundary checks and switching to bounds-checked access on the m_data container in DockGlobalElementModel.

File-Level Changes

Change Details Files
Use bounds-checked container access for m_data
  • Replaced m_data.value(...) with m_data.at(...) in getMenus
  • Replaced m_data.value(...) with m_data.at(...) in data(...)
panels/dock/taskmanager/dockglobalelementmodel.cpp
Tighten boundary check on row index in data()
  • Changed condition from index.row() > m_data.size() to index.row() >= m_data.size() to avoid out-of-range access
panels/dock/taskmanager/dockglobalelementmodel.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

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, BLumia

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

@BLumia BLumia merged commit 79236ae into linuxdeepin:master Sep 5, 2025
9 of 10 checks passed
@BLumia BLumia deleted the close-crash branch September 5, 2025 05:33
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