From 1ba6cb4afb999ae857dd767e447d4aaeeef75157 Mon Sep 17 00:00:00 2001 From: zhanghongyuan Date: Fri, 18 Sep 2026 06:23:37 +0800 Subject: [PATCH] fix: trim file content before comparison in getUrl to fix string matching bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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-devicecontrol/src/drivercontrol/utils.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp b/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp index 9fc154e17..d4bba3eab 100644 --- a/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp +++ b/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp @@ -254,11 +254,17 @@ bool Utils::isDpkgLocked() 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"; } - 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"; } else {