Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/controls/tabbar.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -245,6 +245,10 @@ void Tabbar::closeRightTabs(const QString &filePath)

void Tabbar::updateTab(int index, const QString &filePath, const QString &tabName)
{
if (index < 0 || index >= m_tabPaths.count()) {
return;
Comment on lines +248 to +249

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Save-as paths diverge from tabs

When a save-as update finds a registered wrapper but the old path has no matching tab index, Tabbar::updateTab() returns without changing the tab, while Window::updateSaveAsFileName() and Window::updateSabeAsFileNameTemp() continue updating the wrapper path and map key. The wrapper then points to the new path while the tab retains the old one.

Make the save-as callers stop updating the wrapper and map when the tab update cannot be performed.

Prompt for AI agents
In `src/controls/tabbar.cpp` at lines 248-249:

**Save-as paths diverge from tabs**

When a save-as update finds a registered wrapper but the old path has no matching tab index, `Tabbar::updateTab()` returns without changing the tab, while `Window::updateSaveAsFileName()` and `Window::updateSabeAsFileNameTemp()` continue updating the wrapper path and map key. The wrapper then points to the new path while the tab retains the old one.

Make the save-as callers stop updating the wrapper and map when the tab update cannot be performed.

}

// 适配助记符 '&' 后设置文本
setTabText(index, tabName);
m_tabPaths[index] = filePath;
Expand Down Expand Up @@ -705,7 +709,9 @@ QSize Tabbar::tabSizeHint(int index) const

//计算每个tab平均宽度 返回 100到160
int aveargeWidth = 160;
aveargeWidth = total / DTabBar::count();
if (DTabBar::count() > 0) {
aveargeWidth = total / DTabBar::count();
}

if (aveargeWidth >= 160) {
aveargeWidth = 160;
Expand Down Expand Up @@ -791,6 +797,10 @@ void Tabbar::handleTabReleased(int index)
void Tabbar::handleTabIsRemoved(int index)
{
//qDebug() << "handleTabIsRemoved" << index;
if (index < 0 || index >= m_tabPaths.count()) {
return;
}

const QString filePath = m_tabPaths.at(index);
Window *window = static_cast<Window *>(this->window());
m_tabPaths.removeAt(index);
Expand Down
6 changes: 5 additions & 1 deletion src/thememodule/themelistmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -47,6 +47,10 @@ QVariant ThemeListModel::data(const QModelIndex &index, int role) const
{
const int r = index.row();

if (!index.isValid() || r < 0 || r >= m_themes.count()) {
return QVariant();
}

const QString &name = m_themes.at(r).first;
const QString &path = m_themes.at(r).second;

Expand Down
104 changes: 104 additions & 0 deletions tests/src/controls/ut_tabbar_fuzz383519.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

// 回归用例:PMS BUG#383519【安全专项】【模糊测试】deepin-editor 存在 4 个安全问题。
// 与安全团队 fuzz 用例一一对应:
// fuzz_tabbar.cpp/tabbar_update_tab_fuzz() -> Tabbar::updateTab() 越界写 (CWE-787)
// fuzz_themelistmodel.cpp/themelistmodel_data_fuzz() -> ThemeListModel::data() 越界读 (CWE-125)
// fuzz_tabbar.cpp/tabbar_tab_size_hint_fuzz() -> Tabbar::tabSizeHint() 除零 (CWE-369)
// fuzz_tabbar.cpp/tabbar_handle_tab_is_removed_fuzz() -> Tabbar::handleTabIsRemoved() 越界读 (CWE-125)

#include <gtest/gtest.h>

Check warning on line 12 in tests/src/controls/ut_tabbar_fuzz383519.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "../src/controls/tabbar.h"

Check warning on line 13 in tests/src/controls/ut_tabbar_fuzz383519.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "../src/controls/tabbar.h" not found.
#include "../src/thememodule/themelistmodel.h"

Check warning on line 14 in tests/src/controls/ut_tabbar_fuzz383519.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "../src/thememodule/themelistmodel.h" not found.
#include "../src/widgets/window.h"

Check warning on line 15 in tests/src/controls/ut_tabbar_fuzz383519.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "../src/widgets/window.h" not found.
#include "src/stub.h"

Check warning on line 16 in tests/src/controls/ut_tabbar_fuzz383519.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "src/stub.h" not found.

namespace fuzz383519 {

void removeWrapper_stub()
{
}

}

using namespace fuzz383519;

// 用例1:updateTab() 对越界 index 直接写 m_tabPaths[index]/m_tabTruePaths[index](CWE-787 越界写)
TEST(UT_Tabbar_Fuzz383519, updateTab_outOfRange)
{
Tabbar *tab = new Tabbar;

// 空 tabbar 上传越界 index:负值、超出尾部、超大值
tab->updateTab(-1, "/tmp/fuzz383519.txt", "fuzz");
tab->updateTab(3, "/tmp/fuzz383519.txt", "fuzz");
tab->updateTab(100000, "/tmp/fuzz383519.txt", "fuzz");
EXPECT_EQ(0, tab->count());
EXPECT_TRUE(tab->m_tabPaths.isEmpty());
EXPECT_TRUE(tab->m_tabTruePaths.isEmpty());

// 正向对照:合法 index 行为不变
tab->addTab("/tmp/fuzz383519.txt", "fuzz");
tab->updateTab(0, "/tmp/fuzz383519_other.txt", "fuzz2");
EXPECT_EQ(1, tab->count());
EXPECT_EQ(QString("/tmp/fuzz383519_other.txt"), tab->m_tabPaths.value(0));

tab->deleteLater();
}

// 用例2:data() 对越界 row 直接 m_themes.at(r)(CWE-125 越界读)
TEST(UT_Tabbar_Fuzz383519, themelistmodelData_outOfRange)
{
ThemeListModel model;
const int rows = model.rowCount(QModelIndex());

// 无效 QModelIndex(row == -1)
EXPECT_FALSE(model.data(QModelIndex(), ThemeListModel::ThemeName).isValid());
// row 越过尾部:QAbstractListModel::index() 对越界行返回无效索引
EXPECT_FALSE(model.data(model.index(rows, 0), ThemeListModel::ThemeName).isValid());
EXPECT_FALSE(model.data(model.index(rows + 3, 0), ThemeListModel::ThemePath).isValid());
EXPECT_FALSE(model.data(model.index(rows + 100000, 0), ThemeListModel::ThemeName).isValid());
Comment on lines +59 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Model row bound is untested

When the row-bound guard is later removed or weakened, model.index(rows, 0) and the larger-row variants are invalid indexes, so data() returns at !index.isValid() before the row-range check; the regression still passes if the r >= m_themes.count() guard is removed, leaving valid out-of-range rows untested.

Add a test that passes data() a valid QModelIndex whose row is outside m_themes and verifies that it returns an invalid QVariant.

Prompt for AI agents
In `tests/src/controls/ut_tabbar_fuzz383519.cpp` at lines 59-61:

**Model row bound is untested**

When the row-bound guard is later removed or weakened, `model.index(rows, 0)` and the larger-row variants are invalid indexes, so `data()` returns at `!index.isValid()` before the row-range check; the regression still passes if the `r >= m_themes.count()` guard is removed, leaving valid out-of-range rows untested.

Add a test that passes `data()` a valid `QModelIndex` whose row is outside `m_themes` and verifies that it returns an invalid `QVariant`.


// 正向对照:合法行返回主题名(环境未安装主题文件时跳过)
if (rows > 0) {
EXPECT_TRUE(model.data(model.index(0, 0), ThemeListModel::ThemeName).isValid());
}
}

// 用例3:tabSizeHint() 在 count()==0 时 total / count() 整数除零(CWE-369)
TEST(UT_Tabbar_Fuzz383519, tabSizeHint_divideByZero)
{
Tabbar *tab = new Tabbar;

const QSize size = tab->tabSizeHint(0);
EXPECT_GE(size.width(), 110);
EXPECT_LE(size.width(), 160);

tab->deleteLater();
}

// 用例4:handleTabIsRemoved() 对越界 index 直接 m_tabPaths.at(index)(CWE-125 越界读)
TEST(UT_Tabbar_Fuzz383519, handleTabIsRemoved_outOfRange)
{
// 打桩隔离 Window::removeWrapper 依赖,聚焦越界防护
Stub stub;
stub.set(ADDR(Window, removeWrapper), removeWrapper_stub);

Tabbar *tab = new Tabbar;

// 空 tabbar 上移除:index 0 与越界值
tab->handleTabIsRemoved(0);
tab->handleTabIsRemoved(3);
EXPECT_EQ(0, tab->count());
EXPECT_TRUE(tab->m_tabPaths.isEmpty());

// 正向对照:走真实移除信号路径(removeTab -> tabIsRemoved -> handleTabIsRemoved)
tab->addTab("/tmp/fuzz383519.txt", "fuzz");
EXPECT_EQ(1, tab->count());
tab->removeTab(0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Valid removal control fails

When the test runs its valid-index control using removeTab(0) without a tabIsRemoved emission, handleTabIsRemoved_outOfRange expects m_tabPaths to be empty after tab->removeTab(0), but ordinary removeTab removes the displayed tab without emitting the custom tabIsRemoved signal connected to handleTabIsRemoved; the path remains and the test fails with the correct bounds check.

Exercise the actual tabIsRemoved signal path (or emit the signal directly) for the valid-index control.

Prompt for AI agents
In `tests/src/controls/ut_tabbar_fuzz383519.cpp` at line 99:

**Valid removal control fails**

When the test runs its valid-index control using `removeTab(0)` without a `tabIsRemoved` emission, `handleTabIsRemoved_outOfRange` expects `m_tabPaths` to be empty after `tab->removeTab(0)`, but ordinary `removeTab` removes the displayed tab without emitting the custom `tabIsRemoved` signal connected to `handleTabIsRemoved`; the path remains and the test fails with the correct bounds check.

Exercise the actual `tabIsRemoved` signal path (or emit the signal directly) for the valid-index control.

EXPECT_EQ(0, tab->count());
EXPECT_TRUE(tab->m_tabPaths.isEmpty());

tab->deleteLater();
}
Loading