Skip to content

fix: strip /sys prefix precisely in isPathExisted - #831

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/pms-bug-bot/308f3865a50e
Oct 10, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/pms-bug-bot/308f3865a50e

Conversation

@add-uos

@add-uos add-uos commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

DeviceInfoManager::isPathExisted used QString::replace("/sys", "") to strip the /sys prefix from a sysfs device path before checking it against cached hwinfo data. replace performs global substitution, deleting every /sys substring in the path rather than only the leading prefix. When a path contains /sys more than once (e.g. /sys/bus/sys/devices), all occurrences are removed, producing an incorrect string and a wrong contains() result. The defect is at deviceinfomanager.cpp:63.

Fix

Replaced the global replace("/sys", "") with a startsWith("/sys") guard followed by remove(0, 4), which strips only the leading 4-character prefix. Any /sys substrings in the middle or end of the path are preserved.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • The target line was introduced in the original isPathExisted implementation (commit d44d9ac9, 2021-10-30) and the replace logic has never been modified since — this fix does not revert any prior bug fix.
  • No external callers are affected: the function signature is unchanged, and for paths with a single leading /sys the behavior is identical to before.

Business Impact Scope

Device Manager's device enable/disable feature uses isPathExisted to validate sysfs device paths against cached hwinfo data. After this fix, device paths containing multiple /sys substrings are matched correctly, preventing false negatives/positives in path validation. Standard /sys/devices/... paths are unaffected.

Verification Suggestion

  1. Verify isPathExisted returns correct results for standard /sys prefix paths (no behavior change).
  2. Verify paths with multiple /sys substrings (e.g. /sys/bus/sys/devices) are no longer over-stripped.
  3. Verify paths without a /sys prefix are unaffected.

根因分析

DeviceInfoManager::isPathExisted 使用 QString::replace("/sys", "") 在检查前剥离 sysfs 设备路径的 /sys 前缀。replace 执行全局替换,会删除路径中所有 /sys 子串而非仅剥离前缀。当路径中 /sys 出现多次(如 /sys/bus/sys/devices)时,所有出现位置均被删除,导致字符串错误、contains() 判断结果错误。缺陷位于 deviceinfomanager.cpp:63。

修复方案

将全局 replace("/sys", "") 替换为 startsWith("/sys") 条件判断 + remove(0, 4),仅剥离开头的 4 字符前缀,保留路径中间或末尾的 /sys 子串。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 目标行在 isPathExisted 原始实现中引入(commit d44d9ac9, 2021-10-30),replace 逻辑此后从未被修改——本次修复不撤销任何历史 bug 修复。
  • 无外部调用者受影响:函数签名未变更,仅含前缀 /sys 的路径行为与修复前一致。

业务影响范围

设备管理器的设备启用/禁用功能通过 isPathExisted 校验 sysfs 设备路径。修复后,含多个 /sys 子串的设备路径将正确匹配,避免因路径被错误修改导致的误判。标准 /sys/devices/... 路径不受影响。

验证建议

  1. 验证标准 /sys 前缀路径的 isPathExisted 返回正确结果(行为不变)。
  2. 验证含多个 /sys 子串的路径(如 /sys/bus/sys/devices)不再被过度剥离。
  3. 验证无 /sys 前缀的路径行为不受影响。

Summary by Sourcery

Bug Fixes:

  • Correct path validation by stripping only a leading /sys prefix and preserving any subsequent /sys segments.

1. Root cause: pathT.replace("/sys", "") uses global replace
   semantics, deleting all "/sys" substrings in the path instead of
   only the leading prefix
2. Fix: replace with startsWith("/sys") + remove(0, 4) to strip only
   the leading 4-char prefix, preserving any "/sys" substrings in the
   middle of the path
3. Impact: paths with a single leading /sys behave identically; paths
   containing multiple /sys substrings are no longer incorrectly
   modified; no signature change, no performance impact

Influence:
1. Verify isPathExisted returns correct result for standard /sys paths
2. Verify paths with multiple /sys substrings are handled correctly
3. Verify paths without /sys prefix still work as before

fix: 修复 isPathExisted 中 /sys 前缀剥离使用全局替换的问题

1. 根因:pathT.replace("/sys", "") 使用全局替换语义,会删除路径中
   所有 "/sys" 子串而非仅剥离前缀
2. 方案:改用 startsWith("/sys") + remove(0, 4) 仅剥离开头 4 字符
   前缀,保留路径中间的 "/sys" 子串
3. 影响:仅含前缀 /sys 的路径行为不变;含多个 /sys 子串的路径不再
   被错误修改;无签名变更,无性能影响

Influence:
1. 验证标准 /sys 路径的 isPathExisted 返回正确结果
2. 验证含多个 /sys 子串的路径处理正确
3. 验证无 /sys 前缀的路径行为不受影响

PMS: BUG-4
@sourcery-ai

sourcery-ai Bot commented Oct 10, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 days and 3 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 10, 2026

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

Reviewer's Guide

Fixes DeviceInfoManager::isPathExisted so only a leading /sys prefix is removed, preserving embedded occurrences and producing correct cached hwinfo path matches while leaving non-prefixed and standard paths unchanged.

Flow diagram for precise sysfs prefix stripping

flowchart TD
    A["isPathExisted(path)"] --> B["pathT = path"]
    B --> C{"pathT.startsWith(/sys)"}
    C -- Yes --> D["pathT.remove(0, 4)"]
    C -- No --> E["pathT unchanged"]
    D --> F["hwinfo.contains(pathT)"]
    E --> F
    F --> G["return exists"]
Loading

File-Level Changes

Change Details Files
Restrict sysfs prefix removal to the beginning of the path instead of globally replacing every occurrence.
  • Guard stripping with a leading /sys check.
  • Remove exactly the four-character prefix while preserving later /sys substrings.
  • Pass the resulting path to the cached hwinfo contains() check.
deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinfomanager.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
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

@add-uos

add-uos commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit e18e692 into linuxdeepin:master Oct 10, 2026
29 of 33 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