Skip to content

Commit 96c0307

Browse files
committed
fix: prevent crash from invalid indexes in window monitoring
Add validity checks for model indexes and window pointers to prevent crashes when accessing data from window monitoring models. The crash occurred in treeland environment due to accessing invalid indexes or null window pointers, particularly in the `data()` methods of `AbstractWindowMonitor` and `RoleCombineModel`, and when matching windows in `TaskManager::handleWindowAdded`. Log: Fixed a crash in the dock's window monitoring system by adding null and index validity checks Influence: 1. Test dock stability under treeland by switching between multiple windows rapidly 2. Verify dock does not crash when adding/removing windows dynamically 3. Test with invalid model states (empty models, invalid indexes) 4. Verify window matching still works correctly for active apps 5. Test combination models with various role mappings fix: 修复窗口监控中无效索引导致的崩溃问题 在窗口监控模型中增加模型索引和窗口指针的有效性检查,防止因访问 无效索引或空窗口指针导致崩溃。该崩溃在 treeland 环境下偶发,主要 由 `AbstractWindowMonitor` 和 `RoleCombineModel` 的数据访问方法以及 `TaskManager::handleWindowAdded` 中的窗口匹配逻辑引起。 Log: 修复了 dock 窗口监控系统中因缺少空指针和索引有效性检查导致的崩溃 问题 Influence: 1. 在 treeland 环境下快速切换多个窗口测试 dock 稳定性 2. 验证动态添加/删除窗口时 dock 不会崩溃 3. 测试空模型和无效索引等异常状态 4. 验证活跃应用的窗口匹配功能正常 5. 测试多种角色映射的组合模型
1 parent 9d8b608 commit 96c0307

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

panels/dock/taskmanager/abstractwindowmonitor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -76,10 +76,15 @@ void AbstractWindowMonitor::requestWindowsView(const QModelIndexList &indexes) c
7676

7777
QVariant AbstractWindowMonitor::data(const QModelIndex &index, int role) const
7878
{
79+
if (!index.isValid())
80+
return QVariant();
81+
7982
auto pos = index.row();
80-
if (pos >= m_trackedWindows.size())
83+
if (pos < 0 || pos >= m_trackedWindows.size())
84+
return QVariant();
85+
auto window = m_trackedWindows.value(pos, nullptr);
86+
if (!window)
8187
return QVariant();
82-
auto window = m_trackedWindows[pos];
8388

8489
switch (role) {
8590
case TaskManager::WinIdRole:

panels/dock/taskmanager/rolecombinemodel.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -342,6 +342,10 @@ int RoleCombineModel::columnCount(const QModelIndex &parent) const
342342

343343
QVariant RoleCombineModel::data(const QModelIndex &index, int role) const
344344
{
345+
if (!index.isValid() || !sourceModel()) {
346+
return QVariant();
347+
}
348+
345349
if (m_minorRolesMap.contains(role)) {
346350
auto majorKey = qMakePair(index.row(), index.column());
347351
auto mapping = m_indexMap.value(majorKey, qMakePair(-1, -1));
@@ -355,7 +359,12 @@ QVariant RoleCombineModel::data(const QModelIndex &index, int role) const
355359

356360
return m_minor->data(m_minor->index(row, column), m_minorRolesMap[role]);
357361
} else {
358-
return sourceModel()->data(sourceModel()->index(index.row(), index.column()), role);
362+
auto sourceIndex = sourceModel()->index(index.row(), index.column());
363+
if (!sourceIndex.isValid()) {
364+
return QVariant();
365+
}
366+
367+
return sourceModel()->data(sourceIndex, role);
359368
}
360369
}
361370

panels/dock/taskmanager/taskmanager.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,11 @@ void TaskManager::handleWindowAdded(QPointer<AbstractWindow> window)
327327

328328
// TODO: remove below code and use use model replaced.
329329
QModelIndexList res;
330-
if (m_activeAppModel) {
331-
res = m_activeAppModel->match(m_activeAppModel->index(0, 0), TaskManager::WinIdRole, window->id());
330+
if (m_activeAppModel && m_activeAppModel->rowCount() > 0) {
331+
auto startIndex = m_activeAppModel->index(0, 0);
332+
if (startIndex.isValid()) {
333+
res = m_activeAppModel->match(startIndex, TaskManager::WinIdRole, window->id());
334+
}
332335
}
333336

334337
QSharedPointer<DesktopfileAbstractParser> desktopfile = nullptr;

0 commit comments

Comments
 (0)