Skip to content

fix(device): remove redundant loop in network device generation#701

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-369057-remove-redundant-network-loop
Jul 7, 2026
Merged

fix(device): remove redundant loop in network device generation#701
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-369057-remove-redundant-network-loop

Conversation

@add-uos

@add-uos add-uos commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Remove the outer loop iterating lstDevice in generatorNetworkDevice, as inner operations do not depend on the loop variable, fixing incorrect network device filtering.

移除generatorNetworkDevice中遍历lstDevice的多余外层循环,内层操作
不依赖循环变量,修复网络设备过滤异常的问题。

Log: 修复网络设备生成中循环冗余导致的过滤异常
PMS: BUG-369057
Influence: 修复后网络设备列表生成逻辑正确,避免无线网卡等设备被错误过滤。

Summary by Sourcery

Bug Fixes:

  • Resolve incorrect network device filtering that could wrongly exclude interfaces such as wireless adapters by eliminating an unnecessary iteration over the existing device list.

Remove the outer loop iterating lstDevice in generatorNetworkDevice,
as inner operations do not depend on the loop variable, fixing
incorrect network device filtering.

移除generatorNetworkDevice中遍历lstDevice的多余外层循环,内层操作
不依赖循环变量,修复网络设备过滤异常的问题。

Log: 修复网络设备生成中循环冗余导致的过滤异常
PMS: BUG-369057
Influence: 修复后网络设备列表生成逻辑正确,避免无线网卡等设备被错误过滤。
@sourcery-ai

sourcery-ai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors network device generation to remove a redundant outer loop over lstDevice and perform network-device filtering and creation once per lshw entry, fixing incorrect filtering of devices like WLAN adapters.

Flow diagram for updated network device generation logic

flowchart TD
    A[generatorNetworkDevice iterate lshw entries] --> B[Read logicalNameLshw and macAddressLshw]
    B --> C{isValidLogicalName}
    C -->|no| A
    C -->|yes| D{isValidMAC}
    D -->|no| A
    D -->|yes| E{logicalNameLshw contains wlan and hasWlan}
    E -->|yes| A
    E -->|no| F[Create DeviceNetwork instance]
    F --> G[device.setInfoFromLshw]
    G --> H[Append device to lstDevice]
    H --> I{logicalNameLshw contains wlan}
    I -->|yes| J[set hasWlan true]
    I -->|no| A
    J --> A
Loading

File-Level Changes

Change Details Files
Refactor network device generation loop to operate once per lshw entry instead of redundantly nesting over existing devices, correcting the filtering logic.
  • Remove the outer for-loop iterating over lstDevice in generatorNetworkDevice, leaving a single pass over the lshw iterator
  • Hoist MAC address extraction and validation, logical-name validation, and WLAN de-duplication checks out of the removed loop so they execute once per lshw record
  • Create and append a new DeviceNetwork instance directly after passing validations, and update hasWlan when a WLAN device is detected
deepin-devicemanager/src/GenerateDevice/DeviceGenerator.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

@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 reviewed your changes and they look great!


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
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码成功修复了遍历容器时修改容器引发的未定义行为及设备重复添加的致命逻辑错误
逻辑完全正确且无任何安全漏洞,得满分

■ 【详细分析】

  • 1.语法逻辑 完全正确 ✓

原代码在 DeviceGenerator::generatorNetworkDevice() 中错误地嵌套了遍历 lstDevice 的内层循环,并在循环体内执行 lstDevice.append(device),违反了 C++ 标准中“边遍历边修改容器”的严格禁忌,会导致迭代器失效或死循环。修复后移除了多余的内层循环,将设备追加逻辑正确平铺至外层 lshw 设备遍历中,彻底消除了未定义行为和重复添加问题。

  • 2.代码质量 良好 ✓

修复后的代码消除了无用的嵌套层级和未使用的内层迭代器 itDevice,将 macAddressLshw 变量的作用域调整至与使用处平齐,逻辑结构更加清晰,符合常规编码规范。

  • 3.代码性能 高效 ✓

原代码因死循环或无限重复分配内存会导致极度恶劣的性能表现甚至卡死。修复后时间复杂度恢复为正常的 O(N)(N 为 lshw 网络设备数量),内存分配次数与实际设备数严格一致,无性能问题。

  • 4.代码安全 存在0个安全漏洞 ✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码逻辑仅涉及 Qt 容器操作、基础字符串校验与对象实例化,不涉及外部命令执行、越界读写或敏感信息泄露,整体安全。

■ 【改进建议代码示例】

// 建议使用智能指针管理 DeviceNetwork 对象的生命周期,避免潜在的手动 delete 遗漏导致内存泄漏
void DeviceGenerator::generatorNetworkDevice()
{
    // ... 前置代码 ...
    for (auto it = lstLshw.begin(); it != lstLshw.end(); ++it) {
        const QString &logicalNameLshw =  (*it)["logical name"];
        const QString &macAddressLshw =  (*it)["serial"];
        
        if (!isValidLogicalName(logicalNameLshw))
            continue;
        if (!isValidMAC(macAddressLshw))
            continue;
        if (logicalNameLshw.contains("wlan", Qt::CaseInsensitive) && hasWlan) //common sense: one PC only have 1 wlan device
            continue;

        // 使用 std::unique_ptr 或 QScopedPointer 进行安全管理
        std::unique_ptr<DeviceNetwork> device(new DeviceNetwork());
        device->setInfoFromLshw(*it);
        lstDevice.append(device.release()); // 释放所有权给 lstDevice
        if (logicalNameLshw.contains("wlan", Qt::CaseInsensitive))
            hasWlan = true;
    }
    // ... 后置代码 ...
}

@add-uos

add-uos commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit ee81b7c into linuxdeepin:master Jul 7, 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