Fix(battery): get battery info error.#660
Conversation
-- In Qt 6, when using QProcess::start(), you must separate the program name from the argument list for the command to execute correctly. Log: fix issue Bug: https://pms.uniontech.com/bug-view-361071.html
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: GongHeng2017 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates the battery info retrieval to use QProcess::start() with a separate program and argument list, ensuring the Sequence diagram for updated battery info retrieval using QProcesssequenceDiagram
participant CmdTool
participant QProcess
participant upower
CmdTool->>QProcess: start("upower", QStringList() << "--dump")
QProcess->>upower: execute upower --dump
QProcess-->>CmdTool: waitForFinished(-1)
QProcess-->>CmdTool: readAllStandardOutput()
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Consider using a finite timeout in
waitForFinished()and checking its return value to avoid potential hangs ifupowerblocks or never completes. - It may be helpful to log or handle
process.error()and stderr output whenupowerfails, so battery info failures can be diagnosed more easily.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using a finite timeout in `waitForFinished()` and checking its return value to avoid potential hangs if `upower` blocks or never completes.
- It may be helpful to log or handle `process.error()` and stderr output when `upower` fails, so battery info failures can be diagnosed more easily.
## Individual Comments
### Comment 1
<location path="deepin-devicemanager/src/GenerateDevice/CmdTool.cpp" line_range="1322" />
<code_context>
- process.start(cmd);
-
+ QProcess process;
+ process.start("upower", QStringList() << "--dump");
// 获取命令执行结果
process.waitForFinished(-1);
</code_context>
<issue_to_address>
**issue:** Consider handling QProcess start/finish failures and exit codes.
`start()` and `waitForFinished(-1)` are used without checking whether the process actually started or exited successfully. Since this drives system power information, please verify `process.error() == QProcess::UnknownError` after `start()` and check `process.exitStatus()` / `process.exitCode()` after `waitForFinished()` so you don’t proceed with invalid `powerInfo` when `upower` is missing or fails.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| process.start(cmd); | ||
|
|
||
| QProcess process; | ||
| process.start("upower", QStringList() << "--dump"); |
There was a problem hiding this comment.
issue: Consider handling QProcess start/finish failures and exit codes.
start() and waitForFinished(-1) are used without checking whether the process actually started or exited successfully. Since this drives system power information, please verify process.error() == QProcess::UnknownError after start() and check process.exitStatus() / process.exitCode() after waitForFinished() so you don’t proceed with invalid powerInfo when upower is missing or fails.
deepin pr auto review你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff代码。本次修改主要是对 总体来说,这次修改的方向是非常正确的,显著提升了代码的安全性和健壮性。以下是针对各个维度的详细审查意见和改进建议: 1. 代码安全 - ⭐⭐⭐⭐⭐ (提升显著)
2. 语法逻辑 - ⭐⭐⭐⭐ (良好,但有改进空间)
3. 代码性能 - ⭐⭐⭐ (需优化)
4. 代码质量 - ⭐⭐⭐⭐ (优秀,细节可微调)
💡 综合改进后的代码建议结合以上分析,我为你提供一版改进后的代码,修复了潜在的阻塞问题,增加了错误处理,并提升了代码的现代感: QMap<QString, QMap<QString, QString>> CmdTool::getCurPowerInfo()
{
qCDebug(appLog) << "Getting current power info from upower.";
QString powerInfo;
QMap<QString, QMap<QString, QString>> map;
QProcess process;
// 改进1:使用现代 C++ 初始化列表,更简洁
process.start("upower", {"--dump"});
// 改进2:设置合理的超时时间(例如 5 秒),避免无限期阻塞主线程
if (!process.waitForFinished(5000)) {
qCWarning(appLog) << "Failed to execute upower or timeout:" << process.errorString();
process.kill(); // 确保进程被终止
process.waitForFinished(); // 等待清理完成
return map; // 返回空 map 或根据业务逻辑处理错误
}
// 改进3:检查进程退出码,确保命令执行成功
if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
qCWarning(appLog) << "upower command exited with error, code:" << process.exitCode();
return map;
}
// 获取命令执行结果
powerInfo = process.readAllStandardOutput();
// ... 后续解析逻辑 ...
return map;
}总结:你的原始 Diff 修改已经解决了一个核心的安全隐患(命令注入),是非常棒的改进。在此基础上,通过增加超时控制和退出状态检查,可以让代码在生产环境中更加健壮和可靠。 |
-- In Qt 6, when using QProcess::start(), you must separate the program name from the argument list for the command to execute correctly.
Log: fix issue
Bug: https://pms.uniontech.com/bug-view-361071.html
Summary by Sourcery
Bug Fixes: