Skip to content

fix: remove FileHandler backend and add null checks#139

Merged
18202781743 merged 1 commit intolinuxdeepin:masterfrom
18202781743:master
Feb 5, 2026
Merged

fix: remove FileHandler backend and add null checks#139
18202781743 merged 1 commit intolinuxdeepin:masterfrom
18202781743:master

Conversation

@18202781743
Copy link
Contributor

@18202781743 18202781743 commented Feb 5, 2026

The changes remove the FileHandler backend functionality from the
value handler creation process and add null pointer checks for
ConfigGetter instances. The FileHandler backend was causing issues
with directory writing operations, leading to potential data corruption
or configuration problems. By removing this backend, the system now
relies solely on the DBusHandler for configuration management, ensuring
consistent behavior across the application.

Additionally, multiple null pointer checks have been added when
creating ConfigGetter instances in various UI components (MainWindow,
Content, HistoryDialog) to prevent crashes when manager creation fails.
These checks provide graceful error handling with appropriate warning
messages.

Log: Fixed configuration management issues by removing problematic file
backend

Influence:

  1. Test configuration value retrieval through the UI to ensure no
    regression
  2. Verify that reset operations work correctly for configuration keys
  3. Test context menu functionality in the configuration editor
  4. Validate history dialog operations with various configuration
    resources
  5. Ensure error cases are handled gracefully with appropriate warnings
  6. Test configuration operations with both valid and invalid resource
    paths

fix: 移除FileHandler后端并添加空指针检查

本次修改移除了值处理器创建过程中的FileHandler后端功能,并为ConfigGetter
实例添加了空指针检查。FileHandler后端会导致目录写入操作出现问题,可能引
发数据损坏或配置问题。通过移除此后端,系统现在仅依赖DBusHandler进行配置
管理,确保应用程序行为的一致性。

此外,在多个UI组件(MainWindow、Content、HistoryDialog)中创建
ConfigGetter实例时添加了空指针检查,以防止管理器创建失败时程序崩溃。这些
检查提供了优雅的错误处理机制,并输出适当的警告信息。

Log: 通过移除有问题的文件后端修复了配置管理问题

Influence:

  1. 测试通过UI获取配置值,确保功能没有退化
  2. 验证配置键的重置操作是否正常工作
  3. 测试配置编辑器中的上下文菜单功能
  4. 使用各种配置资源验证历史对话框操作
  5. 确保错误情况得到优雅处理并显示适当的警告
  6. 测试使用有效和无效资源路径的配置操作

PMS: BUG-312173
Change-Id: Iae5db6d3701fe579dc4b0af8e4b6a4ae0f7c9e44

Summary by Sourcery

Remove the file-based configuration backend and harden configuration access against null managers.

Bug Fixes:

  • Prevent crashes when configuration manager creation fails in MainWindow, Content, and HistoryDialog by adding null checks and warnings.
  • Eliminate use of the FileHandler backend in ValueHandler to avoid configuration access issues and rely solely on the DBus-based handler.

Enhancements:

  • Improve robustness of configuration-related UI actions (context menus, reset operations, and history view) by validating manager availability before use.

The changes remove the FileHandler backend functionality from the
value handler creation process and add null pointer checks for
ConfigGetter instances. The FileHandler backend was causing issues
with directory writing operations, leading to potential data corruption
or configuration problems. By removing this backend, the system now
relies solely on the DBusHandler for configuration management, ensuring
consistent behavior across the application.

Additionally, multiple null pointer checks have been added when
creating ConfigGetter instances in various UI components (MainWindow,
Content, HistoryDialog) to prevent crashes when manager creation fails.
These checks provide graceful error handling with appropriate warning
messages.

Log: Fixed configuration management issues by removing problematic file
backend

Influence:
1. Test configuration value retrieval through the UI to ensure no
regression
2. Verify that reset operations work correctly for configuration keys
3. Test context menu functionality in the configuration editor
4. Validate history dialog operations with various configuration
resources
5. Ensure error cases are handled gracefully with appropriate warnings
6. Test configuration operations with both valid and invalid resource
paths

fix: 移除FileHandler后端并添加空指针检查

本次修改移除了值处理器创建过程中的FileHandler后端功能,并为ConfigGetter
实例添加了空指针检查。FileHandler后端会导致目录写入操作出现问题,可能引
发数据损坏或配置问题。通过移除此后端,系统现在仅依赖DBusHandler进行配置
管理,确保应用程序行为的一致性。

此外,在多个UI组件(MainWindow、Content、HistoryDialog)中创建
ConfigGetter实例时添加了空指针检查,以防止管理器创建失败时程序崩溃。这些
检查提供了优雅的错误处理机制,并输出适当的警告信息。

Log: 通过移除有问题的文件后端修复了配置管理问题

Influence:
1. 测试通过UI获取配置值,确保功能没有退化
2. 验证配置键的重置操作是否正常工作
3. 测试配置编辑器中的上下文菜单功能
4. 使用各种配置资源验证历史对话框操作
5. 确保错误情况得到优雅处理并显示适当的警告
6. 测试使用有效和无效资源路径的配置操作

PMS: BUG-312173
Change-Id: Iae5db6d3701fe579dc4b0af8e4b6a4ae0f7c9e44
@18202781743 18202781743 requested review from BLumia and mhduiy February 5, 2026 08:28
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Removes the FileHandler backend from ValueHandler::createManager so that only DBusHandler is used, and adds null checks around ConfigGetter creation in several UI components to avoid crashes when manager creation fails and to log clear warnings instead.

Sequence diagram for ConfigGetter creation with null checks in UI components

sequenceDiagram
    actor User
    participant MainWindow
    participant Content
    participant HistoryDialog
    participant ValueHandler
    participant ConfigGetter

    User->>MainWindow: trigger reset command
    MainWindow->>ValueHandler: new ValueHandler(appid, resource, subpath)
    MainWindow->>ValueHandler: createManager()
    ValueHandler-->>MainWindow: ConfigGetter pointer
    alt manager is null
        MainWindow->>MainWindow: log warning Failed to create manager for reset command
        MainWindow-->>User: no reset performed
    else manager is valid
        MainWindow->>ConfigGetter: keyList()
        ConfigGetter-->>MainWindow: keys
        MainWindow->>ConfigGetter: reset(key)
        MainWindow-->>User: reset completed
    end

    User->>Content: open context menu
    Content->>ValueHandler: new ValueHandler(appid, resource, subpath)
    Content->>ValueHandler: createManager()
    ValueHandler-->>Content: ConfigGetter pointer
    alt manager is null
        Content->>Content: log warning Failed to create manager for context menu
    else manager is valid
        Content->>ConfigGetter: value(key)
        ConfigGetter-->>Content: value
        Content->>ConfigGetter: description(key, language)
        ConfigGetter-->>Content: description
    end

    User->>HistoryDialog: open history dialog
    HistoryDialog->>ValueHandler: handler.createManager()
    ValueHandler-->>HistoryDialog: ConfigGetter pointer
    alt manager is null
        HistoryDialog->>HistoryDialog: log warning Failed to create manager for history
    else manager is valid
        HistoryDialog->>ConfigGetter: value(key)
        ConfigGetter-->>HistoryDialog: historical values
    end
Loading

Updated class diagram for ValueHandler createManager backend selection

classDiagram
    class ValueHandler {
        - QString appid
        - QString resource
        - QString subpath
        + ValueHandler(QString appid, QString resource, QString subpath)
        + ConfigGetter* createManager()
    }

    class ConfigGetter {
        <<abstract>>
        + QStringList keyList()
        + QVariant value(QString key)
        + QString description(QString key, QString language)
        + void reset(QString key)
    }

    class DBusHandler {
        + bool createManager(QString appid, QString fileName, QString subpath)
    }

    class FileHandler {
        + bool createManager(QString appid, QString fileName, QString subpath)
    }

    ValueHandler ..> ConfigGetter : createManager
    ConfigGetter <|-- DBusHandler
    ConfigGetter <|-- FileHandler

    %% Updated behavior: only DBusHandler is used in createManager
    ValueHandler ..> DBusHandler : tries_and_on_success_returns
Loading

File-Level Changes

Change Details Files
Ensure UI code safely handles failures when creating configuration managers to prevent crashes and provide warnings instead.
  • Wrap newly created ConfigGetter instances in null checks before using them in MainWindow reset command handling.
  • Add null checks for ConfigGetter in Content context menu creation and reset handling, early-returning on failure with qWarning logs.
  • Add a null check for ConfigGetter in HistoryDialog when opening history entries, logging a warning and aborting processing on failure.
dconfig-center/dde-dconfig-editor/mainwindow.cpp
dconfig-center/dde-dconfig-editor/content.cpp
dconfig-center/dde-dconfig-editor/historydialog.cpp
Remove FileHandler as a fallback backend in ValueHandler and rely solely on DBusHandler, fixing configuration management issues and cleaning up memory correctly on failure.
  • Delete the branch that constructed and used FileHandler as an alternative when DBusHandler manager creation failed.
  • Add deletion of the temporary DBusHandler instance when its createManager call fails to avoid leaks and make failure explicit.
  • Leave createManager returning nullptr with a warning log if no backend can be created, ensuring callers can detect failure via the new null checks.
dconfig-center/common/valuehandler.cpp

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

@deepin-ci-robot
Copy link

deepin pr auto review

以下,以下是对代码diff的详细审查意见:

代码审查报告

1. 语法逻辑审查

问题1:valuehandler.cpp中的内存管理逻辑

-    } else {
-        auto tmp = new FileHandler(this);
-        if (tmp->createManager(appid, fileName, subpath)) {
-            qDebug() << QString("using FileHandler to get value for appid=%1, resource=%2, subpath=%3.").arg(appid, fileName, subpath);
-            return tmp;
-        }
+        delete tmp;  // 创建失败,释放内存

问题

  • 删除了else分支中的FileHandler创建逻辑,但保留了delete tmp
  • tmp变量在if分支作用域中定义,在else分支中不可见,会导致编译错误
  • 如果createManager失败,tmp指针没有被正确管理

建议修改

if (tmp->createManager(appid, fileName, subpath)) {
    return tmp;
}
delete tmp;  // 创建失败,释放内存

问题2:mainwindow.cpp中的冗余检查

if (!manager) {
    qWarning() << "Failed to create manager for history";
    return;
}
if (manager) {

问题

  • HistoryDialog::HistoryDialog中存在冗余的if (manager)检查
  • 已经检查了!manager,后续的if (manager)是多余的

建议修改

if (!manager) {
    qWarning() << "Failed to create manager for history";
    return;
}
// 直接使用manager,不需要额外的if检查

2. 代码质量审查

优点

  • 添加了空指针检查,提高了代码健壮性
  • 使用QScopedPointer管理资源,避免了内存泄漏
  • 添加了错误日志输出,便于调试

改进建议

  1. 日志信息统一性

    • 不同位置的日志信息格式不一致,建议统一格式
    • 例如:"Failed to create manager for reset command" vs "Failed to create manager for reset"
  2. 错误处理一致性

    • 所有createManager调用后都应进行空指针检查
    • 建议封装一个通用的错误处理函数

3. 代码性能审查

潜在问题

  • 多次调用createManager可能会带来性能开销
  • Content::onCustomContextMenuRequested中,m_getter->createManager()被调用多次

建议

// 在类成员中缓存manager
void Content::ensureManagerCreated() {
    if (!m_manager) {
        m_manager.reset(m_getter->createManager());
        if (!m_manager) {
            qWarning() << "Failed to create manager";
        }
    }
}

4. 代码安全审查

安全问题

  1. 空指针解引用风险

    • 虽然添加了检查,但某些路径仍可能存在风险
    • 例如:manager.get()->value(key)在检查后使用,但如果多线程环境下可能被修改
  2. 资源泄漏风险

    • valuehandler.cpp中的修改可能导致内存泄漏
    • 如果createManager抛出异常,tmp可能不会被正确释放

建议

  1. 使用RAII模式管理资源
  2. 考虑使用std::unique_ptrQScopedPointer替代裸指针
  3. 添加异常处理机制

5. 综合改进建议

建议的完整修改方案

// valuehandler.cpp
ConfigGetter* ValueHandler::createManager() {
    auto tmp = new FileHandler(this);
    if (tmp->createManager(appid, fileName, subpath)) {
        qDebug() << QString("Using FileHandler for appid=%1, resource=%2, subpath=%3")
                    .arg(appid, fileName, subpath);
        return tmp;
    }
    delete tmp;
    qWarning() << QString("Failed to create value handler for appid=%1, resource=%2, subpath=%3")
                  .arg(appid, fileName, subpath);
    return nullptr;
}

// mainwindow.cpp
void MainWindow::onCustomResourceMenuRequested(const QString &appid, const QString &resource, const QString &subpath) {
    // ... 其他代码 ...
    connect(resetCmdAction, &QAction::triggered, this, [this, appid, resource, subpath] {
        QScopedPointer<ValueHandler> getter(new ValueHandler(appid, resource, subpath));
        QScopedPointer<ConfigGetter> manager(getter->createManager());
        if (!manager) {
            qWarning() << QString("Failed to create manager for reset command (appid=%1, resource=%2, subpath=%3)")
                          .arg(appid, resource, subpath);
            return;
        }
        // ... 使用manager ...
    });
}

总结

  1. 必须修复valuehandler.cpp中的变量作用域问题
  2. 建议优化:统一日志格式,减少冗余检查
  3. 长期改进:考虑使用智能指针替代裸指针,添加异常处理机制

这些改进将提高代码的健壮性、可维护性和安全性。

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In ValueHandler::createManager, consider using a RAII helper (e.g. std::unique_ptr/QScopedPointer) for DBusHandler instead of a raw pointer plus manual delete to make the lifetime and failure paths safer and easier to maintain.
  • In HistoryDialog, after adding the early if (!manager) { ... return; } check, the subsequent if (manager) { ... } is now redundant and can be simplified to reduce nesting.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `ValueHandler::createManager`, consider using a RAII helper (e.g. `std::unique_ptr`/`QScopedPointer`) for `DBusHandler` instead of a raw pointer plus manual `delete` to make the lifetime and failure paths safer and easier to maintain.
- In `HistoryDialog`, after adding the early `if (!manager) { ... return; }` check, the subsequent `if (manager) { ... }` is now redundant and can be simplified to reduce nesting.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, BLumia

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

@18202781743 18202781743 merged commit 671aafd into linuxdeepin:master Feb 5, 2026
21 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