Skip to content

fix(tabbar): guard out-of-range index and division by zero in fuzz cases - #739

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
pppanghu77:fix/383519-fuzz-oob-guard
Nov 20, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
pppanghu77:fix/383519-fuzz-oob-guard

Conversation

@pppanghu77

@pppanghu77 pppanghu77 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

修复(tabbar): 修复模糊测试发现的越界索引与除零问题

Log: 修复安全专项模糊测试发现的标签栏越界写、越界读与除零问题,并补充回归测试
Bug: https://pms.uniontech.com/bug-view-383519.html
PMS: BUG-383519

改动内容 / Changes

  • Tabbar::updateTab() 写入 m_tabPaths/m_tabTruePaths 前增加越界校验,修复 CWE-787 越界写 / Bounds check before writing m_tabPaths/m_tabTruePaths in updateTab() (CWE-787)
  • Tabbar::tabSizeHint() 以 count() > 0 保护除法,修复 CWE-369 除零 / Guard division by DTabBar::count() with count() > 0 (CWE-369)
  • Tabbar::handleTabIsRemoved() 读取 m_tabPaths.at() 前增加越界校验,修复 CWE-125 越界读 / Bounds check before m_tabPaths.at() in handleTabIsRemoved() (CWE-125)
  • ThemeListModel::data() 访问 m_themes 前校验索引有效性,修复 CWE-125 越界读 / Validate index in ThemeListModel::data() before accessing m_themes (CWE-125)
  • 新增模糊测试回归用例(覆盖全部四个问题,含合法索引正向对照)/ Add fuzz regression tests covering all four findings with valid-index positive controls

验证 / Verification

  • 基线(未修复):4 个用例全部确定性复现 —— updateTab 触发 QList::operator[] index out of range;data() 触发 QList::at index out of range;tabSizeHint 触发除零(ASAN FPE,栈帧定位 tabbar.cpp:708);handleTabIsRemoved 触发 QList::at index out of range
  • 修复后:4 个用例全部通过,正向对照(合法索引)行为不变
  • 主程序 Debug 构建通过

Summary by Sourcery

Harden tab and theme model handling against invalid indexes and empty-state calculations.

Bug Fixes:

  • Prevent out-of-range tab path access and writes in tab updates and tab removal handling.
  • Prevent division by zero when calculating tab sizes with no tabs.
  • Return an invalid value instead of reading theme data for invalid model indexes.

Tests:

  • Add regression tests covering all four fuzzing findings and valid-index behavior.

- Add bounds check in Tabbar::updateTab() before writing m_tabPaths/m_tabTruePaths to fix CWE-787 out-of-bounds write
- Guard the division by DTabBar::count() in Tabbar::tabSizeHint() with count() > 0 to fix CWE-369 divide-by-zero
- Add bounds check in Tabbar::handleTabIsRemoved() before reading m_tabPaths.at() to fix CWE-125 out-of-bounds read
- Validate index in ThemeListModel::data() before accessing m_themes to fix CWE-125 out-of-bounds read
- Add fuzz regression tests for all four PMS BUG#383519 findings with valid-index positive controls

修复(tabbar): 修复模糊测试发现的越界索引与除零问题

- Tabbar::updateTab() 写入 m_tabPaths/m_tabTruePaths 前增加越界校验,修复 CWE-787 越界写
- Tabbar::tabSizeHint() 以 count() > 0 保护对 DTabBar::count() 的除法,修复 CWE-369 除零
- Tabbar::handleTabIsRemoved() 读取 m_tabPaths.at() 前增加越界校验,修复 CWE-125 越界读
- ThemeListModel::data() 访问 m_themes 前校验索引有效性,修复 CWE-125 越界读
- 新增覆盖 PMS BUG#383519 全部四个问题的模糊测试回归用例,并含合法索引正向对照

Log: 修复安全专项模糊测试发现的标签栏越界写、越界读与除零问题,并补充回归测试
Bug: https://pms.uniontech.com/bug-view-383519.html
PMS: BUG-383519
@sourcery-ai

sourcery-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Guard Tabbar::updateTab() against invalid indices before updating tab data.

  • src/controls/tabbar.cpp L1, L248-251

Avoid dividing by zero when Tabbar::tabSizeHint() has no tabs.

  • src/controls/tabbar.cpp L712-714

Guard Tabbar::handleTabIsRemoved() against invalid indices before reading the tab path.

  • src/controls/tabbar.cpp L800-803

Validate the model index and row bounds before ThemeListModel::data() accesses theme data.

  • src/thememodule/themelistmodel.cpp L1, L50-53

Tests

Add regression tests for the four fuzz findings, including valid-index behavior checks.

  • tests/src/controls/ut_tabbar_fuzz383519.cpp L1-104

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 found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/controls/tabbar.cpp" line_range="248-249" />
<code_context>

 void Tabbar::updateTab(int index, const QString &filePath, const QString &tabName)
 {
+    if (index < 0 || index >= m_tabPaths.count()) {
+        return;
+    }
+
</code_context>
<issue_to_address>
**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.
</issue_to_address>

### Comment 2
<location path="tests/src/controls/ut_tabbar_fuzz383519.cpp" line_range="99" />
<code_context>
+    // 正向对照:走真实移除信号路径(removeTab -> tabIsRemoved -> handleTabIsRemoved)
+    tab->addTab("/tmp/fuzz383519.txt", "fuzz");
+    EXPECT_EQ(1, tab->count());
+    tab->removeTab(0);
+    EXPECT_EQ(0, tab->count());
+    EXPECT_TRUE(tab->m_tabPaths.isEmpty());
</code_context>
<issue_to_address>
**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.
</issue_to_address>

### Comment 3
<location path="tests/src/controls/ut_tabbar_fuzz383519.cpp" line_range="59-61" />
<code_context>
+    // 无效 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());
+
+    // 正向对照:合法行返回主题名(环境未安装主题文件时跳过)
</code_context>
<issue_to_address>
**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`.
</issue_to_address>

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

Comment thread src/controls/tabbar.cpp
Comment on lines +248 to +249
if (index < 0 || index >= m_tabPaths.count()) {
return;

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.

// 正向对照:走真实移除信号路径(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.

Comment on lines +59 to +61
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());

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`.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, pppanghu77

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

@pppanghu77

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 0c90cbe into linuxdeepin:release/eagle Nov 20, 2026
20 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