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
4 changes: 1 addition & 3 deletions src/widgets/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,12 @@ void Window::addTab(const QString &filepath, bool activeTab)
QFile testFile(filepath);
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.

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.

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

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.

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.

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.

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.

qCWarning(appLog) << "No permission to read file:" << filepath << "error:" << testFile.errorString();
QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
DMessageManager::instance()->sendMessage(msgParent, QIcon(":/images/warning.svg"),
QString(tr("You do not have permission to open %1")).arg(filepath));
return;
} else {
qCWarning(appLog) << "Failed to open file:" << filepath << "error:" << testFile.errorString();
}
}

Expand Down
15 changes: 7 additions & 8 deletions tests/widgets_ut/test_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,10 @@ TEST_F(WindowTest, AddTab_ReadOnlyFile_MarksTabReadOnly)
EXPECT_TRUE(waitUntil([w]() { return !w->getFileLoading(); }));
}

TEST_F(WindowTest, AddTab_UnreadableFile_OpenFailsWithGenericError_Continues)
TEST_F(WindowTest, AddTab_UnreadableFile_ShowsPermissionWarning_NoTabCreated)
{
// Arrange: 无读权限文件。Qt 6.8 实测 EACCES 映射为 OpenError(非
// PermissionsError),走"仅告警继续"分支(PermissionsError 提示分支在
// 本 Qt 版本不可达,已记录到 session.skip_reason)
// Arrange: 无读权限文件。修复后任何 open 失败均提示权限不足并 return,
// 不创建标签页
if (geteuid() == 0)
GTEST_SKIP() << "root 用户不受读权限位约束,跳过";
const QString path = createFile(QStringLiteral("secret.txt"));
Expand All @@ -753,10 +752,10 @@ TEST_F(WindowTest, AddTab_UnreadableFile_OpenFailsWithGenericError_Continues)
m_win->addTab(path, true);
QApplication::processEvents();

// Assert: 无权限提示(未走 PermissionsError 分支)且标签照常创建
EXPECT_EQ(m_iconMsgCalls, 0);
EXPECT_EQ(m_tabbar->count(), 1);
EXPECT_EQ(m_tabbar->currentName(), QString("secret.txt"));
// Assert: 权限提示已弹出,未创建标签
EXPECT_EQ(m_iconMsgCalls, 1);
EXPECT_TRUE(m_lastIconMsg.contains(QStringLiteral("permission")));
EXPECT_EQ(m_tabbar->count(), 0);
}

TEST_F(WindowTest, AddTabWithWrapper_RegistersAndShowsEditor)
Expand Down
Loading