Skip to content

fix(input): check device availability#610

Closed
add-uos wants to merge 0 commit intolinuxdeepin:develop/eaglefrom
add-uos:develop/eagle
Closed

fix(input): check device availability#610
add-uos wants to merge 0 commit intolinuxdeepin:develop/eaglefrom
add-uos:develop/eagle

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Mar 3, 2026

check device availability before adding mouse device

log: check device availability before adding mouse device
bug: https://pms.uniontech.com/bug-view-347463.html

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.

Sorry @add-uos, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改主要涉及对设备添加条件的逻辑判断。以下是对这段代码修改的详细审查意见:

1. 语法逻辑

  • 修改点:在原有的 device->bluetoothIsConnected() 条件基础上,增加了 && device->available() 的判断。
  • 分析
    • 这是一个逻辑增强(AND操作)。原代码仅检查设备是否通过蓝牙连接,修改后同时检查设备是否"可用"。
    • 逻辑上是合理的,因为一个设备即使连接了蓝牙,如果处于不可用状态(例如驱动未加载、设备休眠等),也不应该被添加到设备管理器中。
    • 这避免了添加"僵尸"设备(已连接但不可用)到列表中,提高了设备列表的准确性。

2. 代码质量

  • 优点
    • 提高了代码的健壮性(Robustness)。通过增加额外的状态检查,减少了无效对象的生成和管理。
    • 符合防御性编程的原则,确保只有状态完全符合预期的设备才会被处理。
  • 建议
    • 命名规范bluetoothIsConnected()available() 的命名风格不统一。前者是动词开头,后者是形容词开头。建议统一为 isBluetoothConnected()isAvailable(),或者保持原样但确保项目内风格一致。
    • 注释:建议在判断条件处添加简短注释,解释为什么需要同时检查这两个状态,例如:
      // 仅添加已通过蓝牙连接且当前可用的设备
      if (device->bluetoothIsConnected() && device->available()) {

3. 代码性能

  • 分析
    • 增加了一次 available() 函数调用。如果该函数内部实现复杂(例如涉及系统调用或IO操作),可能会带来轻微的性能开销。
    • 考虑到这是设备信息获取阶段,通常不是高频调用的热路径,因此对整体性能影响可以忽略不计。
  • 建议
    • 确保 available() 函数本身是轻量级的。如果它涉及耗时操作,可以考虑缓存其结果,或者确保它只在必要时才进行实际检查。

4. 代码安全

  • 内存安全
    • if 分支中,device 被传递给了 DeviceManager::instance()->addMouseDevice(device)
    • 关键问题:在 else 分支中(diff未显示完整),必须确保 device 被正确释放(delete device),否则会造成内存泄漏。
    • 建议:检查 else 分支(或函数末尾)是否有对应的 delete 操作。如果没有,应添加:
      } else {
          delete device; // 防止内存泄漏
      }
    • 更好的做法:使用智能指针(如 std::unique_ptr<DeviceInput>)管理 device 对象的生命周期,从而自动处理内存释放,避免人为疏忽导致的泄漏。

综合改进建议代码示例

// 使用智能指针自动管理内存
auto device = std::make_unique<DeviceInput>();
device->setInfoFromHwinfo(*it);
device->setHardwareClass("mouse");

// 添加注释说明判断逻辑
// 仅添加已通过蓝牙连接且当前可用的设备
if (device->bluetoothIsConnected() && device->available()) {
    // 如果DeviceManager接管所有权,需使用release()
    DeviceManager::instance()->addMouseDevice(device.release());
    addBusIDFromHwinfo((*it)["SysFS BusID"]);
}
// else分支无需手动delete,智能指针会自动处理

总结

这段修改在逻辑上是正确的,提高了设备管理的准确性。主要需要关注的是:

  1. 确保 else 分支或函数末尾有内存释放逻辑。
  2. 考虑使用智能指针替代原始指针以提高安全性。
  3. 统一函数命名风格并添加必要的注释。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

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.

2 participants