Skip to content

fix: Fix fail to verify bad sectors#176

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
wangrong1069:pr0919-2
Sep 22, 2025
Merged

fix: Fix fail to verify bad sectors#176
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
wangrong1069:pr0919-2

Conversation

@wangrong1069
Copy link
Contributor

@wangrong1069 wangrong1069 commented Sep 19, 2025

In the previous version, the standard output of badblocks was mistakenly used for judgment. The new version uses the standard error output.

Log: Fix fail to verify bad sectors
Bug: https://pms.uniontech.com/bug-view-315835.html

Summary by Sourcery

Add a new executCmd overload to return separate stdout and stderr, update badblocks calls to use stderr for status checks, and correct bad sector verification logic

New Features:

  • Add executCmd overload to capture both standard output and standard error separately

Bug Fixes:

  • Fix badblocks verification by checking standard error for "0/0/0" instead of standard output

Enhancements:

  • Update WorkThread and FixThread to use the new executCmd overload and inspect stderr for block status

Documentation:

  • Document the new executCmd overload in utils.h

@sourcery-ai
Copy link

sourcery-ai bot commented Sep 19, 2025

Reviewer's Guide

This PR introduces a new overloaded Utils::executCmd that separately captures stdout and stderr, updates its declaration, and refactors badblocks invocations in disk operation threads to use stderr output for verifying bad sectors instead of stdout.

File-Level Changes

Change Details Files
Add new executCmd overload capturing both stdout and stderr
  • Implement Utils::executCmd overload to parse command, launch QProcess, and read standard output and error separately
  • Return proc.errorString() and exit code through the error parameter and function return value
basestruct/utils.cpp
basestruct/utils.h
Use stderr output for badblocks result verification
  • Replace existing executCmd calls in WorkThread and FixThread to pass nullptr for stdout and capture stderr
  • Update condition checks from output.indexOf("0/0/0") to stdErr.indexOf("0/0/0")
service/diskoperation/thread.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

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.

Hey there - I've reviewed your changes - here's some feedback:

  • Consolidate the two executCmd overloads (e.g. via default arguments or a single return struct) to avoid code duplication and simplify maintenance.
  • Use nullptr instead of NULL when passing optional stdOut/stdErr pointers to executCmd for modern C++ style consistency.
  • Rename the 'error' out-parameter to something more descriptive (e.g. 'procErrorString') to clearly differentiate it from standard error output.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consolidate the two executCmd overloads (e.g. via default arguments or a single return struct) to avoid code duplication and simplify maintenance.
- Use nullptr instead of NULL when passing optional stdOut/stdErr pointers to executCmd for modern C++ style consistency.
- Rename the 'error' out-parameter to something more descriptive (e.g. 'procErrorString') to clearly differentiate it from standard error output.

## Individual Comments

### Comment 1
<location> `basestruct/utils.cpp:132-135` </location>
<code_context>
+    if (stdOut)
+        *stdOut = proc.readAllStandardOutput().data();
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Direct conversion from QByteArray to QString may cause encoding issues.

Use QString::fromLocal8Bit or QString::fromUtf8 for conversion to handle non-ASCII output correctly, based on the expected encoding.

```suggestion
    if (stdOut)
        *stdOut = QString::fromLocal8Bit(proc.readAllStandardOutput());
    if (stdErr)
        *stdErr = QString::fromLocal8Bit(proc.readAllStandardError());
```
</issue_to_address>

### Comment 2
<location> `basestruct/utils.cpp:115` </location>
<code_context>
     return exitcode;
 }

+int Utils::executCmd(const QString &strCmd, QString *stdOut, QString *stdErr, QString &error)
+{
+    qDebug() << "Utils::executCmd" << strCmd;
</code_context>

<issue_to_address>
**issue (complexity):** Consider refactoring QProcess setup and teardown into a shared helper to eliminate duplication across overloads.

Consider pulling the QProcess‐setup and teardown into one helper, then have both overloads call it. For example:

```cpp
// private helper
static int runProcess(const QString &prog,
                      const QStringList &args,
                      QString *stdOut,
                      QString *stdErr,
                      QString &error)
{
    QProcess proc;
    proc.setProgram(prog);
    proc.setArguments(args);
    proc.start(QIODevice::ReadWrite);
    proc.waitForFinished(-1);

    if (stdOut) *stdOut = proc.readAllStandardOutput();
    if (stdErr) *stdErr = proc.readAllStandardError();
    error = proc.errorString();

    int exitCode = proc.exitCode();
    proc.close();
    qDebug() << "runProcess exitCode:" << exitCode;
    return exitCode;
}
```

Then reduce your two overloads to:

```cpp
int Utils::executCmd(const QString &strCmd,
                     QString &error)
{
    auto args = parseCombinedArgString(strCmd);
    if (args.isEmpty()) { error = "args is empty"; return -1; }
    return runProcess(args.takeFirst(), args, nullptr, nullptr, error);
}

int Utils::executCmd(const QString &strCmd,
                     QString *stdOut,
                     QString *stdErr,
                     QString &error)
{
    auto args = parseCombinedArgString(strCmd);
    if (args.isEmpty()) { error = "args is empty"; return -1; }
    return runProcess(args.takeFirst(), args, stdOut, stdErr, error);
}
```

This removes all duplicated parsing, QProcess setup, and logging while preserving both APIs.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

In the previous version, the standard output of badblocks was
mistakenly used for judgment. The new version uses the standard error
output.

Log: Fix fail to verify bad sectors
Bug: https://pms.uniontech.com/bug-view-315835.html
@deepin-ci-robot
Copy link

deepin pr auto review

代码审查报告

总体评价

这段代码对Utils::executCmd函数进行了重构,增加了新的重载版本,并改进了命令执行的方式。同时,在调用处也相应进行了调整。整体来看,改进提高了代码的灵活性和可维护性,但还有一些可以优化的地方。

具体分析

1. 代码逻辑

  • 优点:

    • 新增的重载函数提供了更灵活的输出控制,可以选择性地获取标准输出和标准错误
    • 使用QProcess直接执行命令,替代了之前的executeCmdWithArtList函数,使逻辑更清晰
  • 问题:

    • Utils::executCmd函数中,错误处理不够完善。当proc.start()失败时,应该立即返回错误码,而不是继续执行后续代码
    • proc.close()调用是多余的,因为waitForFinished(-1)会自动等待进程结束,此时进程已经关闭

2. 代码质量

  • 优点:

    • 函数重载设计合理,提供了不同场景下的调用方式
    • 注释清晰,特别是新增函数的文档注释
  • 问题:

    • 函数名executCmd有拼写错误,应该是executeCmd
    • 变量名stdOutstdErr虽然能理解,但不符合Qt的命名规范,建议改为stdoutstderr
    • thread.cpp中,错误信息被重复定义了stdErrerror,可以简化为一个变量

3. 代码性能

  • 优点:

    • 直接使用QProcess减少了不必要的函数调用层级
    • 使用waitForFinished(-1)确保命令完全执行,避免部分输出丢失
  • 问题:

    • 对于长时间运行的命令,waitForFinished(-1)会导致界面卡顿,可以考虑使用异步方式
    • 没有设置超时机制,如果命令长时间不返回,可能会导致程序挂起

4. 代码安全

  • 优点:

    • 使用QProcess执行命令,相比直接使用system()更安全
    • 命令参数通过setArguments设置,避免了命令注入风险
  • 问题:

    • 没有对输入命令进行合法性检查
    • 没有限制命令执行的超时时间,可能导致资源耗尽

改进建议

  1. 修正函数名拼写错误:
int Utils::executeCmd(const QString &strCmd, QString *stdout, QString *stderr, QString &error)
  1. 改进错误处理:
QProcess proc;
proc.setProgram(prog);
proc.setArguments(args);
if (!proc.start(QIODevice::ReadWrite)) {
    error = proc.errorString();
    return -1;
}
  1. 添加超时机制:
if (!proc.waitForFinished(30000)) { // 30秒超时
    error = "Command execution timeout";
    proc.kill();
    return -1;
}
  1. 简化thread.cpp中的错误处理:
QString error;
int exitcode = Utils::executeCmd(cmd, nullptr, nullptr, error);
  1. 考虑使用异步方式执行命令,避免界面卡顿:
// 可以添加一个异步版本的executeCmd
static void executeCmdAsync(const QString &strCmd, std::function<void(int, QString, QString)> callback);
  1. 添加命令合法性检查:
if (!isValidCommand(prog)) {
    error = "Invalid command";
    return -1;
}
  1. 优化资源管理:
{
    QProcess proc;
    // ... 使用proc
} // proc自动销毁

总结:这次改进提高了代码的灵活性,但在错误处理、性能优化和安全性方面还有提升空间。建议按照上述建议进行优化,使代码更加健壮和安全。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wangrong1069

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

@wangrong1069
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Sep 22, 2025

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit d3dd370 into linuxdeepin:master Sep 22, 2025
16 of 18 checks passed
@wangrong1069 wangrong1069 deleted the pr0919-2 branch September 22, 2025 00:56
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