Skip to content

fix: trim file content before comparison in getUrl to fix string matching bug - #768

Open
add-uos wants to merge 1 commit into
linuxdeepin:masterfrom
add-uos:agent/bug/6e6e0620c196
Open

add-uos wants to merge 1 commit into
linuxdeepin:masterfrom
add-uos:agent/bug/6e6e0620c196

Conversation

@add-uos

@add-uos add-uos commented Sep 17, 2026

Copy link
Copy Markdown
Contributor
  1. Root cause: getUrl() reads ~/url file content via file.readAll() and
    compares it directly with "true" using QString equality. When the file
    contains "true\n" (with a trailing newline, common when users create
    the file manually with echo or editors), the comparison fails, causing
    the function to incorrectly return the pre-production URL even though
    the file content indicates production environment.
  2. Fix: call trimmed() on the result of file.readAll() before comparison
    to strip leading/trailing whitespace and newlines, ensuring "true\n"
    and similar variants correctly match "true". Also add comments explaining
    the design intent of the ~/url file and the meaning of each branch.
  3. Impact: only deepin-devicemanager-server/deepin-devicecontrol/src/
    drivercontrol/utils.cpp getUrl() is affected; no change to logic
    semantics (true = production, non-true = pre-production, file unreadable
    = production default); trimmed() returns a new QString with no side effects.

Log: Fix string comparison bug in getUrl by trimming file content before matching

Influence:

  1. getUrl() correctly returns production URL when ~/url contains "true\n"
  2. getUrl() behavior unchanged for files containing exactly "true" or other values
  3. Design intent of ~/url file is now documented via inline comments

fix: 修复 getUrl 中字符串比较 Bug,对文件内容进行 trim 后再比较

  1. 根因:getUrl() 通过 file.readAll() 读取 ~/url 文件内容后直接与 "true"
    进行 QString 相等比较。当文件内容为 "true\n"(带换行符,用户手动创建
    文件时极为常见)时,比较失败,导致函数错误返回预生产环境 URL,即使
    文件内容表明应使用生产环境。
  2. 方案:对 file.readAll() 的结果调用 trimmed(),去除首尾空白和换行符后
    再比较,确保 "true\n" 等变体能正确匹配 "true"。同时增加注释说明 ~/url
    文件的设计意图和各分支含义。
  3. 影响:仅影响 deepin-devicemanager-server/deepin-devicecontrol/src/
    drivercontrol/utils.cpp 的 getUrl() 方法;不改变逻辑语义(true = 生产
    环境,非 true = 预生产环境,文件不可读 = 生产环境默认值);trimmed()
    返回新的 QString,无副作用。

Log: 修复 getUrl 字符串比较 Bug,对文件内容 trim 后再匹配

Influence:

  1. ~/url 文件包含 "true\n" 时 getUrl() 正确返回生产环境 URL
  2. 文件内容为 "true" 或其他值时 getUrl() 行为不变
  3. ~/url 文件设计意图已通过注释文档化

PMS: V-4865

Summary by Sourcery

Normalize ~/url contents before selecting the driver service environment to fix incorrect URL matching.

Bug Fixes:

  • Ensure production URLs are selected when the ~/url configuration contains true with trailing or leading whitespace.

Enhancements:

  • Document the ~/url environment-selection behavior and its safe default.

…hing bug

1. Root cause: getUrl() reads ~/url file content via file.readAll() and
   compares it directly with "true" using QString equality. When the file
   contains "true\n" (with a trailing newline, common when users create
   the file manually with echo or editors), the comparison fails, causing
   the function to incorrectly return the pre-production URL even though
   the file content indicates production environment.
2. Fix: call trimmed() on the result of file.readAll() before comparison
   to strip leading/trailing whitespace and newlines, ensuring "true\n"
   and similar variants correctly match "true". Also add comments explaining
   the design intent of the ~/url file and the meaning of each branch.
3. Impact: only deepin-devicemanager-server/deepin-devicecontrol/src/
   drivercontrol/utils.cpp getUrl() is affected; no change to logic
   semantics (true = production, non-true = pre-production, file unreadable
   = production default); trimmed() returns a new QString with no side effects.

Log: Fix string comparison bug in getUrl by trimming file content before matching

Influence:
1. getUrl() correctly returns production URL when ~/url contains "true\n"
2. getUrl() behavior unchanged for files containing exactly "true" or other values
3. Design intent of ~/url file is now documented via inline comments

fix: 修复 getUrl 中字符串比较 Bug,对文件内容进行 trim 后再比较

1. 根因:getUrl() 通过 file.readAll() 读取 ~/url 文件内容后直接与 "true"
   进行 QString 相等比较。当文件内容为 "true\n"(带换行符,用户手动创建
   文件时极为常见)时,比较失败,导致函数错误返回预生产环境 URL,即使
   文件内容表明应使用生产环境。
2. 方案:对 file.readAll() 的结果调用 trimmed(),去除首尾空白和换行符后
   再比较,确保 "true\n" 等变体能正确匹配 "true"。同时增加注释说明 ~/url
   文件的设计意图和各分支含义。
3. 影响:仅影响 deepin-devicemanager-server/deepin-devicecontrol/src/
   drivercontrol/utils.cpp 的 getUrl() 方法;不改变逻辑语义(true = 生产
   环境,非 true = 预生产环境,文件不可读 = 生产环境默认值);trimmed()
   返回新的 QString,无副作用。

Log: 修复 getUrl 字符串比较 Bug,对文件内容 trim 后再匹配

Influence:
1. ~/url 文件包含 "true\n" 时 getUrl() 正确返回生产环境 URL
2. 文件内容为 "true" 或其他值时 getUrl() 行为不变
3. ~/url 文件设计意图已通过注释文档化

PMS: V-4865
@deepin-ci-robot

Copy link
Copy Markdown

[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

@sourcery-ai

sourcery-ai Bot commented Sep 17, 2026

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

Reviewer's Guide

Fixes getUrl() environment selection for manually created ~/url files containing values such as "true\n" by trimming read content before comparison, while preserving existing branch semantics and documenting the configuration intent.

Flow diagram for getUrl environment selection

flowchart TD
    A[getUrl] --> B[Open ~/url for reading]
    B -->|open fails| C[Return production URL]
    B -->|open succeeds| D["readAll().trimmed()"]
    D --> E{info equals true}
    E -->|yes| C
    E -->|no| F[Return pre-production URL]
Loading

File-Level Changes

Change Details Files
Normalize the ~/url file value before selecting the driver service environment.
  • Trim leading and trailing whitespace/newlines from file contents before comparing with "true".
  • Preserve production as the default for unreadable files, production for "true", and pre-production for other values.
  • Document the ~/url switch semantics and alignment with the client implementation.
deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.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 found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp" line_range="267" />
<code_context>
     }
-    QString info = file.readAll();
+    // trimmed() 去除首尾空白和换行符,确保 "true\n" 等内容能正确匹配 "true"
+    QString info = file.readAll().trimmed();
     if ("true" == info) {
         return "https://driver.uniontech.com/api/v1/drive/search";
</code_context>
<issue_to_address>
**issue (broader_impact):** The server now treats `"true\n"` or other surrounding-whitespace variants as production, while the client’s `CommonTools::getUrl()` still compares the raw file contents and treats the same file as pre-production. The shared `~/url` configuration therefore selects different driver service environments depending on whether the request goes through the server or the client.

**Triggers:** When `~/url` contains `true` with leading or trailing whitespace, including the documented `true\n` case.

**Suggested fix:** Apply the same trimming behavior in `deepin-devicemanager/src/Tool/commontools.cpp`, or centralize URL selection so both components use the same normalization rule.
</issue_to_address>

### Comment 2
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp" line_range="261" />
<code_context>
+    // - 文件不存在或不可读时,默认使用生产环境 URL(安全默认)
+    // - 文件内容为 "true" 时,使用生产环境 URL
+    // - 文件内容为其他值时,使用预生产环境 URL
+    // 该设计意图与客户端 commontools.cpp 中 CommonTools::getUrl() 保持一致
     QFile file(QDir::homePath() + "/url");
     if (!file.open(QIODevice::ReadOnly)) {
</code_context>
<issue_to_address>
**nitpick:** The new comment claims this implementation’s design intent remains consistent with `CommonTools::getUrl()`, but `CommonTools::getUrl()` does not call `trimmed()` and has different behavior for whitespace-terminated values. The comment falsely documents cross-component consistency and can mislead future maintainers into assuming the client has the same fix.

**Suggested fix:** Update the client implementation as well, or change the comment to state that the implementations currently differ.

```suggestion
    // 该设计意图与客户端 commontools.cpp 中 CommonTools::getUrl() 当前实现存在差异
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

}
QString info = file.readAll();
// trimmed() 去除首尾空白和换行符,确保 "true\n" 等内容能正确匹配 "true"
QString info = file.readAll().trimmed();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (broader_impact): The server now treats "true\n" or other surrounding-whitespace variants as production, while the client’s CommonTools::getUrl() still compares the raw file contents and treats the same file as pre-production. The shared ~/url configuration therefore selects different driver service environments depending on whether the request goes through the server or the client.

Triggers: When ~/url contains true with leading or trailing whitespace, including the documented true\n case.

Suggested fix: Apply the same trimming behavior in deepin-devicemanager/src/Tool/commontools.cpp, or centralize URL selection so both components use the same normalization rule.

// - 文件不存在或不可读时,默认使用生产环境 URL(安全默认)
// - 文件内容为 "true" 时,使用生产环境 URL
// - 文件内容为其他值时,使用预生产环境 URL
// 该设计意图与客户端 commontools.cpp 中 CommonTools::getUrl() 保持一致

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nitpick: The new comment claims this implementation’s design intent remains consistent with CommonTools::getUrl(), but CommonTools::getUrl() does not call trimmed() and has different behavior for whitespace-terminated values. The comment falsely documents cross-component consistency and can mislead future maintainers into assuming the client has the same fix.

Suggested fix: Update the client implementation as well, or change the comment to state that the implementations currently differ.

Suggested change
// 该设计意图与客户端 commontools.cpp 中 CommonTools::getUrl() 保持一致
// 该设计意图与客户端 commontools.cpp 中 CommonTools::getUrl() 当前实现存在差异

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 98 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 98 分,大于 70 分通过阈值。本次变更修复了 getUrl() 函数中字符串比较的 Bug,通过添加 trimmed() 去除文件内容首尾空白符后再比较,确保 "true\n" 等带换行符的内容能正确匹配 "true"。变更范围小、逻辑正确、注释完善,代码质量优秀。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 语法正确,逻辑清晰。trimmed() 是 QByteArray 的有效方法,file.readAll() 返回 QByteArray,调用 trimmed() 后再隐式转换为 QString 赋值给 info 变量,类型链完整。逻辑正确:trimmed() 去除首尾空白和换行符后与 "true" 比较,确保 "true\n"、" true " 等变体能正确匹配。边界条件处理完善:文件不可读时返回生产环境 URL(安全默认),trim 后为 "true" 返回生产环境 URL,其他值返回预生产环境 URL。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 代码结构清晰,注释完整。本次变更新增 6 行注释,详细说明了 ~/url 文件的设计意图、各分支含义以及 trimmed() 的作用,并与客户端 commontools.cpp 中 CommonTools::getUrl() 保持一致。注释质量高,有助于后续维护。建议:可考虑将 URL 配置提取为常量或配置文件,避免硬编码(既有问题,非本次变更引入)。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 性能良好,资源使用合理。trimmed() 的时间复杂度为 O(n),其中 n 为字符串长度,对于配置文件内容(通常几十字节)性能影响可忽略不计。实际上在 QByteArray 上调用 trimmed() 比先转换为 QString 再 trim 更高效,因为避免了不必要的 QString 构造开销。QFile 使用 RAII 机制,函数结束时自动关闭文件,无资源泄漏风险。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 存在0个安全漏洞。本次变更未引入任何安全风险。文件路径来自 QDir::homePath() 而非用户输入,不存在路径遍历风险。URL 为服务端点地址,非敏感凭证信息。trimmed() 操作无副作用,不会导致缓冲区溢出或内存安全问题。变更实际上提升了代码的健壮性,修复了因字符串未 trim 导致的环境判断错误。


💡 改进建议代码示例

// 当前实现已正确修复了 Bug,以下为可选的改进建议(非必须):

QString Utils::getUrl()
{
    // ~/url 文件用于切换驱动查询的环境地址:
    // - 文件不存在或不可读时,默认使用生产环境 URL(安全默认)
    // - 文件内容为 "true" 时,使用生产环境 URL
    // - 文件内容为其他值时,使用预生产环境 URL
    // 该设计意图与客户端 commontools.cpp 中 CommonTools::getUrl() 保持一致
    QFile file(QDir::homePath() + "/url");
    if (!file.open(QIODevice::ReadOnly)) {
        return "https://driver.uniontech.com/api/v1/drive/search";
    }
    // trimmed() 去除首尾空白和换行符,确保 "true\n" 等内容能正确匹配 "true"
    QString info = file.readAll().trimmed();
    // 可选改进:使用大小写不敏感比较,提升容错性
    if (info.compare("true", Qt::CaseInsensitive) == 0) {
        return "https://driver.uniontech.com/api/v1/drive/search";
    } else {
        return "https://pre-driver.uniontech.com/api/v1/drive/search";
    }
}

本报告由 AI 代码审查工具自动生成

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