Skip to content

Conversation

@zorowk
Copy link
Contributor

@zorowk zorowk commented Dec 26, 2025

fix: surface guard against null container in SurfaceListModel::data

SurfaceListModel::data() may be queried before a SurfaceWrapper
is fully attached to a workspace container.

After application startup, ShellHandler::matchOrCreateXdgWrapper()
and ShellHandler::matchOrCreateXwaylandWrapper() add wrappers into
the workspace. At this stage, Workspace::addSurface() is called
with surface->container() still unset, which is expected and
asserted by:

Q_ASSERT(surface && !surface->container());

However, QML may access SurfaceListModel and request
Qt::InitialSortOrderRole before the container is initialized.
This leads to a null pointer dereference when accessing
container->childItems().

Fix this by explicitly checking for a null container in
SurfaceListModel::data() and returning an invalid QVariant
until the surface is fully initialized.

This prevents crashes during early QML evaluation while keeping
the existing initialization order unchanged.

issue: #659
comment: #659 (comment)

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: zorowk

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
Copy link

sourcery-ai bot commented Dec 26, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds defensive null checks in SurfaceListModel::data for the InitialSortOrderRole to avoid dereferencing incompletely initialized QQuickItem instances, and falls back to INT_MAX as the sort order for surfaces whose parent/container is not yet ready.

Class diagram for updated SurfaceListModel::data null-check behavior

classDiagram
    class SurfaceListModel {
        +data(index QModelIndex, role int) QVariant
        +surfaces() QList~Surface*~
    }

    class Surface {
        +parentItem() QQuickItem*
        +container() QQuickItem*
    }

    class QQuickItem {
        +childItems() QList~QQuickItem*~
    }

    class QModelIndex
    class QVariant

    SurfaceListModel --> Surface : uses
    Surface --> QQuickItem : parentItem
    Surface --> QQuickItem : container
    QQuickItem --> QQuickItem : childItems
    SurfaceListModel --> QModelIndex : parameter
    SurfaceListModel --> QVariant : return type
Loading

File-Level Changes

Change Details Files
Harden SurfaceListModel::data against partially initialized surfaces when computing InitialSortOrderRole, falling back to a max-int sort order for unparented/uncontained items.
  • Guard access to surface->parentItem() and return INT_MAX when the parent item is null.
  • Guard access to surface->container() and return INT_MAX when the container is null.
  • Keep using container->childItems().indexOf(surface) to derive the sort index, asserting the index is non-negative for valid cases.
src/surface/surfacecontainer.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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Instead of returning the magic value INT_MAX in multiple places, consider defining a named constant (e.g. kUnreadySurfaceSortOrder) or using std::numeric_limits::max() to improve readability and type safety.
  • Given that container->childItems().indexOf(surface) can theoretically return -1 even after your null checks, it may be safer to replace the Q_ASSERT with a conditional that also falls back to the default sort order rather than crashing in release builds.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of returning the magic value INT_MAX in multiple places, consider defining a named constant (e.g. kUnreadySurfaceSortOrder) or using std::numeric_limits<int>::max() to improve readability and type safety.
- Given that container->childItems().indexOf(surface) can theoretically return -1 even after your null checks, it may be safer to replace the Q_ASSERT with a conditional that also falls back to the default sort order rather than crashing in release builds.

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.

@zorowk
Copy link
Contributor Author

zorowk commented Dec 29, 2025

打开应用之后 ShellHandler::matchOrCreateXdgWrapper 和ShellHandler::matchOrCreateXwaylandWrapper 会添加wrapper到workspace中

void Workspace::addSurface(SurfaceWrapper *surface, int workspaceId)
{
Q_ASSERT(surface && !surface->container() && surface->workspaceId() == -1);

此时的surface->container 还未初始化是空指针

后续qml读取了surfacelistmodel中的数据导致崩溃

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a crash in SurfaceListModel::data that occurs when accessing childItems() on surfaces that have not yet been fully initialized in the scene graph. The fix adds a null check for the container before dereferencing it.

Key Changes:

  • Added null check for surface->container() in the data method to prevent dereferencing null containers
  • Returns an empty QVariant() when container is null as a fallback

SurfaceListModel::data() may be queried before a SurfaceWrapper
is fully attached to a workspace container.

After application startup, ShellHandler::matchOrCreateXdgWrapper()
and ShellHandler::matchOrCreateXwaylandWrapper() add wrappers into
the workspace. At this stage, Workspace::addSurface() is called
with surface->container() still unset, which is expected and
asserted by:

    Q_ASSERT(surface && !surface->container());

However, QML may access SurfaceListModel and request
Qt::InitialSortOrderRole before the container is initialized.
This leads to a null pointer dereference when accessing
container->childItems().

Fix this by explicitly checking for a null container in
SurfaceListModel::data() and returning an invalid QVariant
until the surface is fully initialized.

This prevents crashes during early QML evaluation while keeping
the existing initialization order unchanged.

issue: linuxdeepin#659
comment: linuxdeepin#659 (comment)
@wineee
Copy link
Member

wineee commented Dec 30, 2025

#671 可能是同一个问题

@zorowk
Copy link
Contributor Author

zorowk commented Dec 30, 2025

#671 可能是同一个问题

确实可以 如果将workspace model添加surface调整到container初始化后

@zorowk zorowk closed this Dec 30, 2025
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