Skip to content

fix: explicit layout deletion to avoid dangling pointers - #872

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/pms-bug-bot/c3636d9b28b2
Oct 26, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/pms-bug-bot/c3636d9b28b2

Conversation

@add-uos

@add-uos add-uos commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

fix: explicit layout deletion to avoid dangling pointers

  1. Root cause: hideAll() deleted only the top-level layout via
    delete mp_ScrollWidget->layout(), relying on Qt implicit
    cascading deletion for child layouts. This left six member
    pointers dangling between delete and reassignment, risking
    use-after-free if any code accesses them in between.
  2. Fix: explicitly delete each of the six layout member pointers
    from innermost to outermost, set each to nullptr after deletion,
    replacing the single delete mp_ScrollWidget->layout() call.
  3. Impact: only DriverScanWidget::hideAll() is affected. Behavior
    is unchanged for normal state switching; the fix eliminates
    the dangling pointer window and removes the implicit dependency
    on Qt cascading deletion.

Influence:

  1. Test repeated switching between scanning, scan-failed, and
    network-error states to verify no crash or memory growth
  2. Verify UI layout renders correctly after multiple state switches
  3. Confirm no regression in scanning progress display

fix: 显式删除布局避免悬垂指针

  1. 根因:hideAll() 仅通过 delete mp_ScrollWidget->layout() 删除
    顶层布局,依赖 Qt 隐式级联删除子布局。六个成员指针在
    delete 与重新赋值之间处于悬空状态,若期间被访问将触发
    use-after-free。
  2. 方案:从内到外依次显式 delete 六个布局成员指针并置 nullptr,
    替换原来的 delete mp_ScrollWidget->layout()。
  3. 影响:仅影响 DriverScanWidget::hideAll(),正常状态切换行为
    不变,消除悬垂指针窗口并移除对 Qt 级联删除的隐式依赖。

Influence:

  1. 测试扫描中、扫描失败、网络错误三态反复切换,验证无崩溃
    或内存增长
  2. 验证多次状态切换后 UI 布局正确渲染
  3. 确认扫描进度显示无回归

PMS: BUG-DEF-T19-002

Summary by Sourcery

Eliminate dangling layout pointers in DriverScanWidget::hideAll by explicitly resetting layouts before recreating the UI.

Bug Fixes:

  • Prevent dangling layout pointers during DriverScanWidget state transitions by explicitly clearing all existing layouts before rebuilding them.

Enhancements:

  • Make layout cleanup independent of Qt's implicit child-layout deletion behavior.

1. Root cause: hideAll() deleted only the top-level layout via
   delete mp_ScrollWidget->layout(), relying on Qt implicit
   cascading deletion for child layouts. This left six member
   pointers dangling between delete and reassignment, risking
   use-after-free if any code accesses them in between.
2. Fix: explicitly delete each of the six layout member pointers
   from innermost to outermost, set each to nullptr after deletion,
   replacing the single delete mp_ScrollWidget->layout() call.
3. Impact: only DriverScanWidget::hideAll() is affected. Behavior
   is unchanged for normal state switching; the fix eliminates
   the dangling pointer window and removes the implicit dependency
   on Qt cascading deletion.

Influence:
1. Test repeated switching between scanning, scan-failed, and
   network-error states to verify no crash or memory growth
2. Verify UI layout renders correctly after multiple state switches
3. Confirm no regression in scanning progress display

fix: 显式删除布局避免悬垂指针

1. 根因:hideAll() 仅通过 delete mp_ScrollWidget->layout() 删除
   顶层布局,依赖 Qt 隐式级联删除子布局。六个成员指针在
   delete 与重新赋值之间处于悬空状态,若期间被访问将触发
   use-after-free。
2. 方案:从内到外依次显式 delete 六个布局成员指针并置 nullptr,
   替换原来的 delete mp_ScrollWidget->layout()。
3. 影响:仅影响 DriverScanWidget::hideAll(),正常状态切换行为
   不变,消除悬垂指针窗口并移除对 Qt 级联删除的隐式依赖。

Influence:
1. 测试扫描中、扫描失败、网络错误三态反复切换,验证无崩溃
   或内存增长
2. 验证多次状态切换后 UI 布局正确渲染
3. 确认扫描进度显示无回归

PMS: BUG-DEF-T19-002
@sourcery-ai

sourcery-ai Bot commented Oct 23, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

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

@sourcery-ai

sourcery-ai Bot commented Oct 23, 2026

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

Reviewer's Guide

DriverScanWidget::hideAll() now explicitly deletes and nulls all six layout members from inner to outer instead of relying on Qt’s cascading deletion, preventing transient dangling pointers while preserving normal state-switching behavior.

Sequence diagram for explicit layout replacement in hideAll

sequenceDiagram
    participant DriverScanWidget
    participant LayoutMembers
    participant QtLayouts

    DriverScanWidget->>LayoutMembers: hideAll()
    DriverScanWidget->>QtLayouts: delete mp_HLayoutPic
    DriverScanWidget->>DriverScanWidget: mp_HLayoutPic = nullptr
    DriverScanWidget->>QtLayouts: delete mp_HLayoutscLabel
    DriverScanWidget->>DriverScanWidget: mp_HLayoutscLabel = nullptr
    DriverScanWidget->>QtLayouts: delete mp_HLayoutscProgress
    DriverScanWidget->>DriverScanWidget: mp_HLayoutscProgress = nullptr
    DriverScanWidget->>QtLayouts: delete mp_HLayoutInfoLabel
    DriverScanWidget->>DriverScanWidget: mp_HLayoutInfoLabel = nullptr
    DriverScanWidget->>QtLayouts: delete mp_VLayout
    DriverScanWidget->>DriverScanWidget: mp_VLayout = nullptr
    DriverScanWidget->>QtLayouts: delete mp_HLayout
    DriverScanWidget->>DriverScanWidget: mp_HLayout = nullptr
    DriverScanWidget->>QtLayouts: new QHBoxLayout()
    DriverScanWidget->>QtLayouts: new QHBoxLayout()
    DriverScanWidget->>QtLayouts: new QHBoxLayout()
Loading

File-Level Changes

Change Details Files
Replace implicit top-level layout deletion with explicit innermost-to-outermost cleanup and nullification of all layout member pointers.
  • Delete each of the six layout members explicitly in dependency order.
  • Set every deleted layout pointer to nullptr before rebuilding the layout hierarchy.
  • Recreate the initial horizontal layouts using the now-cleared member pointers.
deepin-devicemanager/src/Widget/DriverScanWidget.cpp
Preserve existing state-switching behavior while eliminating the dangling-pointer window in hideAll().
  • Keep widget hiding and subsequent layout reconstruction flow unchanged.
  • Focus validation on repeated scanning, failure, and network-error transitions, including layout rendering and progress display.
deepin-devicemanager/src/Widget/DriverScanWidget.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


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交修复了 DriverScanWidget::hideAll() 中的悬垂指针问题,通过显式删除各子布局并置空指针, 替代原先仅删除父布局的做法。代码语法正确、逻辑清晰,注释完整,无安全漏洞,无性能问题。 四维度均通过,OCR 审查与启发式扫描均未发现问题。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

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

建议: 无


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

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

建议: 可考虑封装一个工具函数(如 deleteAndNull(Layout*& ptr))来减少重复代码,但对于仅 6 处的场景,当前写法可接受。


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

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

建议: 无


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

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

建议: 无


💡 改进建议代码示例

// 当前代码已为正确的修复方式,无需额外修改。
// 如需进一步优化可考虑封装工具函数:
template<typename T>
void safeDelete(T*& ptr) {
    delete ptr;
    ptr = nullptr;
}
// 使用:safeDelete(mp_HLayoutPic);

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

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@add-uos

add-uos commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit 85250f1 into linuxdeepin:master Oct 26, 2026
17 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