Skip to content

fix: block tab creation on file open failure - #738

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/339655
Nov 20, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/339655

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

fix: block tab creation on file open failure

  1. Root cause: QFile::open returns OpenError(5) for permission-
    denied files, not PermissionsError(13); the else-if condition
    never matches, falling through to create a blank tab
  2. Fix: change else-if(PermissionsError) to else so any open
    failure shows a warning and returns, preventing tab creation
  3. Impact: all file-open failure paths now properly block tab
    creation; normal readable files are unaffected

Log: 修复打开无权限文件时创建空白标签页的问题

Influence:

  1. Test opening a file with 0000 permissions — should show warning, no tab
  2. Test opening a file with 0644 permissions — should open normally
  3. Test opening a non-existent file — should not create blank tab

fix: 修复打开无权限文件时创建空白标签页的问题

  1. 根因:QFile::open 对权限不足文件返回 OpenError(5) 而非
    PermissionsError(13),else-if 条件永不命中,落入不 return
    的分支导致创建空白标签页
  2. 方案:将 else-if(PermissionsError) 改为 else,任何 open
    失败均提示并 return,阻止标签页创建
  3. 影响:所有文件打开失败路径均正确阻止标签页创建,正常文件不受影响

Log: 修复打开无权限文件时创建空白标签页的问题

Influence:

  1. 测试打开 0000 权限文件 — 应提示权限不足,不创建标签页
  2. 测试打开 0644 权限文件 — 应正常打开
  3. 测试打开不存在的文件 — 不应创建空白标签页

PMS: BUG-339655

Summary by Sourcery

Block tab creation when opening a file fails while preserving normal file-opening behavior.

Bug Fixes:

  • Prevent blank tabs from being created when any file-open attempt fails, including permission-denied files.

Tests:

  • Update window tests to verify unreadable files show a permission warning and do not create a tab.

@pengfeixx
pengfeixx marked this pull request as draft November 18, 2026 11:11
@sourcery-ai

sourcery-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Treat any failure to open the requested file as a blocking error, show a warning, and return before creating a tab.

  • src/widgets/window.cpp L720, L726 (removed lines)

Tests

Update the unreadable-file test to verify that the warning is shown and no tab is created.

  • tests/widgets_ut/test_window.cpp L739-742, L755-758

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

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

## Individual Comments

### Comment 1
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Missing files leave blank tabs**

When the requested supported path does not exist when `addTab` checks it, `fileInfo.exists()` skips the open-failure branch, so `Window::addTab` creates a tab and starts loading the missing path; the load failure leaves the user with a blank tab.

Reject missing paths before creating the tab, rather than skipping the open check when `fileInfo.exists()` is false.
</issue_to_address>

### Comment 2
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Failed loads leave blank tabs**

When the file is removed or becomes unreadable after the probe succeeds but before the asynchronous load opens it, `Window::addTab` commits the tab after closing the probe file, then `FileLoadThread` fails to open the path and the load-failure handler leaves the editor empty; the caller sees a blank tab.

Handle asynchronous load failures by removing or rejecting the tab and wrapper instead of leaving an empty tab.
</issue_to_address>

### Comment 3
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Open errors get wrong explanation**

When an existing supported file fails to open for a reason other than insufficient permissions, `addTab` routes every `QFile::open` failure through the permission-specific log and message, so callers are told they lack permission even when another I/O error caused the failure.

Use the permission-specific log and message only for `PermissionsError`, and report other open failures with an appropriate generic error.

Also at `src/widgets/window.cpp:721-724`.
</issue_to_address>

### Comment 4
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Existing document is not activated**

When a file already open in another window becomes unreadable before the user opens it again, `addTab` returns on the failed open before `StartManager::checkPath` can find and activate the existing tab, so the user sees a warning instead of the existing document and its loaded or unsaved content.

Check for and activate an already-open path before returning on a failed read probe.
</issue_to_address>

### Comment 5
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Pending files bypass read check**

When a multi-file selection includes an unreadable supported file after the first file, `Window::openFile` sends later files through `addPendingTab`, which bypasses the open check; activating the placeholder later starts a load that fails and leaves an empty tab.

Apply the open check to files before creating pending tabs.
</issue_to_address>

### Comment 6
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Unreadable-file test fails**

When the window tests run as a non-root user where the test file's permission bits prevent reading, `addTab` now warns and returns without creating a tab, but `AddTab_UnreadableFile_OpenFailsWithGenericError_Continues` expects no warning and one tab, so the test fails.

Update the test to expect the warning and the absence of a tab under enforced unreadable-file permissions.
</issue_to_address>

### Comment 7
<location path="src/widgets/window.cpp" line_range="720" />
<code_context>
             if (testFile.open(QIODevice::ReadOnly)) {
                 testFile.close();
-            } else if (testFile.error() == QFileDevice::PermissionsError) {
+            } else {
                 qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
                 QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
</code_context>
<issue_to_address>
**Failed opens leave empty windows**

When an unreadable file is opened through the new-window route, `StartManager::openFilesInWindow` creates and shows a window before calling `Window::addTab`; when the new early return prevents the tab from being added, the `void` call gives the caller no way to close that window. Opening an unreadable file in this route leaves an empty window visible.

Have the caller detect a failed open and close or avoid showing the newly created window.
</issue_to_address>

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

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

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 · Missing files leave blank tabs

When the requested supported path does not exist when addTab checks it, fileInfo.exists() skips the open-failure branch, so Window::addTab creates a tab and starts loading the missing path; the load failure leaves the user with a blank tab.

Reject missing paths before creating the tab, rather than skipping the open check when fileInfo.exists() is false.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Missing files leave blank tabs**

When the requested supported path does not exist when `addTab` checks it, `fileInfo.exists()` skips the open-failure branch, so `Window::addTab` creates a tab and starts loading the missing path; the load failure leaves the user with a blank tab.

Reject missing paths before creating the tab, rather than skipping the open check when `fileInfo.exists()` is false.

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

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 · Failed loads leave blank tabs

When the file is removed or becomes unreadable after the probe succeeds but before the asynchronous load opens it, Window::addTab commits the tab after closing the probe file, then FileLoadThread fails to open the path and the load-failure handler leaves the editor empty; the caller sees a blank tab.

Handle asynchronous load failures by removing or rejecting the tab and wrapper instead of leaving an empty tab.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Failed loads leave blank tabs**

When the file is removed or becomes unreadable after the probe succeeds but before the asynchronous load opens it, `Window::addTab` commits the tab after closing the probe file, then `FileLoadThread` fails to open the path and the load-failure handler leaves the editor empty; the caller sees a blank tab.

Handle asynchronous load failures by removing or rejecting the tab and wrapper instead of leaving an empty tab.

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

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 · Open errors get wrong explanation

When an existing supported file fails to open for a reason other than insufficient permissions, addTab routes every QFile::open failure through the permission-specific log and message, so callers are told they lack permission even when another I/O error caused the failure.

Use the permission-specific log and message only for PermissionsError, and report other open failures with an appropriate generic error.

Also at src/widgets/window.cpp:721-724.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Open errors get wrong explanation**

When an existing supported file fails to open for a reason other than insufficient permissions, `addTab` routes every `QFile::open` failure through the permission-specific log and message, so callers are told they lack permission even when another I/O error caused the failure.

Use the permission-specific log and message only for `PermissionsError`, and report other open failures with an appropriate generic error.

Also at `src/widgets/window.cpp:721-724`.

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

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 · Existing document is not activated

When a file already open in another window becomes unreadable before the user opens it again, addTab returns on the failed open before StartManager::checkPath can find and activate the existing tab, so the user sees a warning instead of the existing document and its loaded or unsaved content.

Check for and activate an already-open path before returning on a failed read probe.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Existing document is not activated**

When a file already open in another window becomes unreadable before the user opens it again, `addTab` returns on the failed open before `StartManager::checkPath` can find and activate the existing tab, so the user sees a warning instead of the existing document and its loaded or unsaved content.

Check for and activate an already-open path before returning on a failed read probe.

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

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 · Pending files bypass read check

When a multi-file selection includes an unreadable supported file after the first file, Window::openFile sends later files through addPendingTab, which bypasses the open check; activating the placeholder later starts a load that fails and leaves an empty tab.

Apply the open check to files before creating pending tabs.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Pending files bypass read check**

When a multi-file selection includes an unreadable supported file after the first file, `Window::openFile` sends later files through `addPendingTab`, which bypasses the open check; activating the placeholder later starts a load that fails and leaves an empty tab.

Apply the open check to files before creating pending tabs.

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Unreadable-file test fails

When the window tests run as a non-root user where the test file's permission bits prevent reading, addTab now warns and returns without creating a tab, but AddTab_UnreadableFile_OpenFailsWithGenericError_Continues expects no warning and one tab, so the test fails.

Update the test to expect the warning and the absence of a tab under enforced unreadable-file permissions.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Unreadable-file test fails**

When the window tests run as a non-root user where the test file's permission bits prevent reading, `addTab` now warns and returns without creating a tab, but `AddTab_UnreadableFile_OpenFailsWithGenericError_Continues` expects no warning and one tab, so the test fails.

Update the test to expect the warning and the absence of a tab under enforced unreadable-file permissions.

Comment thread src/widgets/window.cpp
if (testFile.open(QIODevice::ReadOnly)) {
testFile.close();
} else if (testFile.error() == QFileDevice::PermissionsError) {
} else {

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 · Failed opens leave empty windows

When an unreadable file is opened through the new-window route, StartManager::openFilesInWindow creates and shows a window before calling Window::addTab; when the new early return prevents the tab from being added, the void call gives the caller no way to close that window. Opening an unreadable file in this route leaves an empty window visible.

Have the caller detect a failed open and close or avoid showing the newly created window.

Prompt for AI agents
In `src/widgets/window.cpp` at line 720:

**Failed opens leave empty windows**

When an unreadable file is opened through the new-window route, `StartManager::openFilesInWindow` creates and shows a window before calling `Window::addTab`; when the new early return prevents the tab from being added, the `void` call gives the caller no way to close that window. Opening an unreadable file in this route leaves an empty window visible.

Have the caller detect a failed open and close or avoid showing the newly created window.

1. Root cause: QFile::open returns OpenError(5) for permission-denied
   files, not PermissionsError(13); the else-if condition never matches
2. Fix: change else-if to else so any open failure shows a warning and
   returns, preventing tab creation
3. Impact: all file-open failure paths now properly block tab creation

Influence:
1. Test opening files with read permission denied
2. Test opening normal readable files to verify no regression
3. Test opening non-existent files for error handling

fix: 阻止文件打开失败时创建标签页

1. 根因:QFile::open 对权限不足文件返回 OpenError(5) 而非
   PermissionsError(13),else-if 条件永不命中
2. 方案:将 else-if 改为 else,任何 open 失败均提示并 return
3. 影响:所有文件打开失败路径均正确阻止标签页创建

Influence:
1. 测试打开无读权限文件
2. 测试打开正常可读文件验证无回归
3. 测试打开不存在文件的错误处理

Log: fix blank tab created when opening files without permission
Log: 修复打开无权限文件时创建空白标签页的问题
PMS: BUG-339655
@pengfeixx
pengfeixx marked this pull request as ready for review November 20, 2026 01:00
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

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

@pengfeixx

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit c3f00cc into linuxdeepin:master Nov 20, 2026
20 checks passed
@pengfeixx
pengfeixx deleted the agent/bugfix/339655 branch November 20, 2026 02:41
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