Skip to content

fix: null-check DStyle cast in view widgets - #928

Closed
add-uos wants to merge 2 commits into
linuxdeepin:masterfrom
add-uos:agent/bug/de73d8bfece7
Closed

add-uos wants to merge 2 commits into
linuxdeepin:masterfrom
add-uos:agent/bug/de73d8bfece7

Conversation

@add-uos

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

Copy link
Copy Markdown
Contributor

Root Cause Analysis

dynamic_cast<DStyle*>(DApplication::style()) return value was dereferenced without null check in DriverListViewDelegate::paint(), DriverListView::drawRow(), and LogViewHeaderView::sectionSizeHint(). When the application style is not a DStyle derivative (e.g. QApplication embedding or third-party style), the cast returns nullptr, causing SIGSEGV on every item paint. The codebase has 14 such cast sites; 11 already have null guards, these 3 were missed.

Fix Approach

Add null check before dereferencing the DStyle pointer in all 3 locations. When style is null, fall back to default values (margin=10, radius=8) and emit a qCWarning diagnostic log. This preserves rendering continuity in non-DStyle environments rather than early-returning, which would skip item drawing entirely.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Only adds null-check branches and default value assignments; no existing logic modified
  • The 11 existing null-check guards in the same codebase validate this pattern as safe and established

Business Impact Scope

Affects device list view rendering, log view header sizing, and row drawing in the device manager UI. In normal DStyle environments, behavior is unchanged. In non-DStyle environments (unit test, QApplication embedding), crashes are prevented and UI renders with fallback values.

Verification Suggestion

Verify device list view and log view render correctly under normal DStyle environment (no regression). Optionally test under QApplication-only environment to confirm no crash.


根因分析

DriverListViewDelegate::paint()、DriverListView::drawRow() 和 LogViewHeaderView::sectionSizeHint() 中 dynamic_cast<DStyle*>(DApplication::style()) 返回值未判空即解引用。当应用样式非 DStyle 派生(如 QApplication 嵌入或第三方样式)时,cast 返回 nullptr,每次条目绘制即段错误。代码库共 14 处同类调用,11 处已有判空保护,此 3 处为遗漏。

修复方案

在 3 处添加 DStyle 指针判空,为空时回退默认值(margin=10, radius=8)并输出 qCWarning 诊断日志。回退默认值而非早期 return,以保证非 DStyle 环境下渲染连续性。

改动安全评估

代码安全评估

  • 风险等级: 低
  • 仅增加判空分支和默认值赋值,不修改现有逻辑
  • 代码库中已有 11 处相同模式的判空保护,验证此模式安全且成熟

业务影响范围

影响设备管理器中设备列表视图渲染、日志视图表头尺寸计算和行绘制。正常 DStyle 环境下行为不变。非 DStyle 环境(单元测试、QApplication 嵌入)下可防止崩溃并以默认值渲染。

验证建议

验证 DStyle 环境下设备列表视图和日志视图渲染正常(无回归)。可选测试 QApplication-only 环境确认不崩溃。

Summary by Sourcery

Prevent view rendering crashes in non-DStyle environments while preserving normal DStyle behavior.

Bug Fixes:

  • Prevent crashes when device and log views run with a non-DStyle application style by using safe fallback rendering values.

Enhancements:

  • Add diagnostic warnings when the application style cannot be treated as DStyle.

@sourcery-ai

sourcery-ai Bot commented Oct 26, 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 1 day and 16 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: add-uos

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 Oct 26, 2026

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

Reviewer's Guide

Adds null checks around three previously unsafe DStyle casts, preserving device-list and log-header rendering with default margin/radius values and warnings in non-DStyle environments while leaving normal DStyle behavior unchanged.

Flow diagram for safe DStyle metric fallback

flowchart TD
    A["View rendering requests style metric"] --> B["dynamic_cast<DStyle *>(DApplication::style())"]
    B --> C{style available?}
    C -->|Yes| D["pixelMetric(PM_ContentsMargins or PM_FrameRadius)"]
    C -->|No| E["Use default margin=10 or radius=8"]
    E --> F["qCWarning(appLog)"]
    D --> G["Continue rendering or sizing"]
    F --> G
Loading

File-Level Changes

Change Details Files
Prevent crashes when the application style is not a DStyle derivative by using guarded fallback metrics.
  • Initialize radius to 8 and use the DStyle metric when the cast succeeds.
  • Initialize margin to 10 and use the DStyle metric when available.
  • Emit qCWarning diagnostics when DStyle is unavailable while continuing to render or size views.
deepin-devicemanager/src/Widget/DriverListView.cpp
deepin-devicemanager/src/Widget/DriverListViewDelegate.cpp
deepin-devicemanager/src/Widget/logviewheaderview.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 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交对3个视图控件文件中的 DStyle dynamic_cast 结果增加空指针检查,使用默认值兜底并通过 qCWarning 记录异常路径,有效防止了空指针解引用崩溃。语法逻辑正确,性能无影响,安全方面无漏洞。仅存在版权头格式书写不当的轻微质量问题。

🔍 详细分析

1. 语法逻辑 ✅

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

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

建议: []


2. 代码质量 ✅

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

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

建议: ["将版权头统一修改为 '// Copyright (C) 2019-2026 UnionTech Software Technology Co.,Ltd' 或 '// Copyright (C) 2019 ~ 2026 UnionTech Software Technology Co.,Ltd'"]


3. 代码性能 ✅

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

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

建议: []


4. 代码安全 🔒

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

🔐 发现 0 个安全漏洞

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

建议: []


💡 改进建议代码示例

// 修复版权头格式(以 DriverListView.cpp 为例): // 修改前: // Copyright (C) 2019-2026 ~ 2020 UnionTech Software Technology Co.,Ltd // // 修改后: // Copyright (C) 2019-2026 UnionTech Software Technology Co.,Ltd

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

1. dynamic_cast<DStyle*>(DApplication::style()) return value was
   dereferenced without null check, causing SIGSEGV when the app
   style is not a DStyle derivative
2. Add null check with fallback default values (margin=10, radius=8)
   in paint(), drawRow() and sectionSizeHint() to prevent crash
3. Emit qCWarning(appLog) when style is null for diagnostics
4. Consistent with the 11 existing null-check guards in the codebase

Log: fix potential crash in non-DStyle style environments

Influence:
1. Verify device list view renders correctly under DStyle environment
2. Verify no crash when running under non-DStyle (e.g. QApplication) environment
3. Verify log view header sizing is correct

fix: 修复视图控件中 DStyle 转换未判空导致的崩溃

1. dynamic_cast<DStyle*>(DApplication::style()) 返回值未判空即解引用,
   非派生自 DStyle 的样式环境下导致段错误
2. 在 paint()、drawRow()、sectionSizeHint() 中添加判空并回退默认值
   (margin=10, radius=8),防止崩溃
3. style 为空时输出 qCWarning(appLog) 日志便于排查
4. 与代码库中已有 11 处判空保护模式风格统一

Log: 修复非 DStyle 样式环境下的潜在崩溃问题

Influence:
1. 验证 DStyle 环境下设备列表视图渲染正常
2. 验证非 DStyle 环境(如 QApplication)下不崩溃
3. 验证日志视图表头尺寸计算正确
The license-check CI (SPDX Header Validation) requires the copyright year
range of touched sources to end with the current year. Extend the legacy
copyright headers of DriverListView.cpp and DriverListViewDelegate.cpp
(2019 ~ 2020) so 2026 is covered and the SPDX summary reports 0 failures.
@add-uos
add-uos force-pushed the agent/bug/de73d8bfece7 branch from 5d05863 to 71a335e Compare October 27, 2026 03:45
@deepin-bot

deepin-bot Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.75
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #956

@add-uos add-uos closed this Oct 30, 2026
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