Skip to content

fix: add null guards and check svg file open in DDropdownMenu - #632

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix/ddropdownmenu-guards
Oct 8, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix/ddropdownmenu-guards

Conversation

@add-uos

@add-uos add-uos commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

问题

  1. setCurrentAction():menuList[i]->menu()->actions() 对 menu() 无判空——高亮菜单含无子菜单的顶层 action("None"),调用即空指针崩溃(与 ab891dd 在 setCurrentTextOnly 中修复的 56765 同类缺陷,该方法未同步加固);m_menu 本身也无判空。
  2. setCurrentTextOnly():m_menu->actions() 无 m_menu 判空——deleteMenu()/setMenu(nullptr) 之后调用即崩。
  3. setSvgColor():QFile::open 无失败处理,资源缺失时静默渲染空白箭头图标;SetSVGBackColor() 内 before_color 赋值后未使用。

修复

  • setCurrentAction():入口对 m_menu 判空,循环内跳过无子菜单的 action;
  • setCurrentTextOnly():遍历前对 m_menu 判空(保留 setText);
  • setSvgColor():检查 open() 失败时告警并返回空 pixmap;删除未使用的 before_color。

背景

该问题由 AI 辅助单测(PMS 缺陷热点补强)读码审查发现。

Summary by Sourcery

Harden DDropdownMenu against null menus and unavailable SVG resources.

Bug Fixes:

  • Prevent dropdown menu crashes when the menu or an action’s submenu is unavailable.
  • Handle missing SVG arrow resources gracefully by warning and returning an empty pixmap.

Enhancements:

  • Remove an unused SVG color variable.

- setCurrentAction(): guard against a null m_menu and skip top-level
  actions that have no submenu before calling menu()->actions(); a
  submenu-less action (e.g. the 'None' entry) crashed there.
- setCurrentTextOnly(): guard m_menu, which is null after
  deleteMenu()/setMenu(nullptr).
- setSvgColor(): check QFile::open() and return an empty pixmap instead
  of silently rendering a blank arrow icon; drop the unused
  before_color variable in SetSVGBackColor.

Log:
Influence: ddropdownmenu widget
@sourcery-ai

sourcery-ai Bot commented Sep 30, 2026

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

Reviewer's Guide

Adds null guards to dropdown menu selection paths to prevent crashes after menu removal or when actions have no submenu, and makes SVG loading failures observable with a safe empty-pixmap fallback while removing an unused variable.

Sequence diagram for guarded dropdown menu selection

sequenceDiagram
    participant Caller
    participant DDropdownMenu
    participant QMenu
    participant QAction

    Caller->>DDropdownMenu: setCurrentAction(pAct)
    alt m_menu is null
        DDropdownMenu->>DDropdownMenu: setText(pAct->text())
        DDropdownMenu-->>Caller: return
    else m_menu exists
        DDropdownMenu->>QMenu: actions()
        DDropdownMenu->>QAction: setChecked(true)
        loop menu actions
            DDropdownMenu->>QAction: menu()
            alt subMenu is null
                DDropdownMenu-->>DDropdownMenu: continue
            else subMenu exists
                DDropdownMenu->>QMenu: actions()
                DDropdownMenu->>QAction: setChecked(false)
            end
        end
    end

    Caller->>DDropdownMenu: setCurrentTextOnly(name)
    alt m_menu exists
        DDropdownMenu->>QMenu: actions()
        DDropdownMenu->>DDropdownMenu: setCheckedExclusive(ac,name)
    end
    DDropdownMenu->>DDropdownMenu: setText(name)
Loading

Sequence diagram for SVG loading failure handling

sequenceDiagram
    participant Caller
    participant DDropdownMenu
    participant QFile

    Caller->>DDropdownMenu: setSvgColor(color)
    DDropdownMenu->>QFile: open(QIODevice::ReadOnly)
    alt open succeeds
        DDropdownMenu->>QFile: readAll()
        DDropdownMenu-->>Caller: colored QPixmap
    else open fails
        DDropdownMenu->>DDropdownMenu: qWarning()
        DDropdownMenu-->>Caller: QPixmap()
    end
Loading

File-Level Changes

Change Details Files
Harden menu-selection state updates against missing menus and submenu-less actions.
  • Return safely and preserve displayed text when the root menu is null.
  • Skip top-level actions without submenus before accessing submenu actions.
  • Guard text-only selection traversal while retaining the requested text update.
src/widgets/ddropdownmenu.cpp
Handle missing SVG resources explicitly and remove dead state.
  • Check SVG resource opening, log a warning, and return an empty pixmap on failure.
  • Remove the unused previous-color variable.
src/widgets/ddropdownmenu.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

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@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 8, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit bd15465 into linuxdeepin:master Oct 8, 2026
14 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