Skip to content

Fix: improve QProcess usage for Qt6 compatibility#622

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
GongHeng2017:202603201954-dev-eagle-fix
Mar 23, 2026
Merged

Fix: improve QProcess usage for Qt6 compatibility#622
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
GongHeng2017:202603201954-dev-eagle-fix

Conversation

@GongHeng2017
Copy link
Contributor

@GongHeng2017 GongHeng2017 commented Mar 20, 2026

  • Refactored QProcess::start() calls in parseEDID() to use the modern API by separating program name and arguments instead of shell string concatenation.
  • Added return value validation for waitForFinished() to handle process execution failures gracefully.
  • Applied QString::fromLocal8Bit() to standard output reading to ensure proper text encoding handling.
  • These changes improve code robustness, security (avoiding shell injection risks), and compatibility with Qt6 best practices.

Log: fix issue
Bug: https://pms.uniontech.com/bug-view-353603.html

Summary by Sourcery

Improve EDID parsing by modernizing QProcess usage and adding robustness around hexdump output handling.

Bug Fixes:

  • Prevent failures and undefined behavior when hexdump execution fails or returns empty output during EDID parsing.

Enhancements:

  • Use argument-based QProcess::start and proper text decoding for hexdump output to align with Qt6 best practices and improve security.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 20, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors QProcess usage in CommonTools::parseEDID() to use the argument-based start() API, adds robust error handling and logging around process execution and empty output, and ensures stdout is decoded with QString::fromLocal8Bit() for better Qt6 compatibility and robustness.

Sequence diagram for updated CommonTools_parseEDID QProcess handling

sequenceDiagram
    participant CommonTools
    participant QProcess
    participant hexdump
    participant FileSystem

    CommonTools->>CommonTools: parseEDID(allEDIDS, input, isHW)
    loop for each edid in allEDIDS
        alt isHW is true
            CommonTools->>QProcess: QProcess()
            CommonTools->>QProcess: start(hexdump, [edid])
        else isHW is false
            CommonTools->>QProcess: QProcess()
            CommonTools->>QProcess: start(hexdump, [-C, edid])
        end

        QProcess->>hexdump: execute with arguments
        hexdump->>FileSystem: read edid file
        FileSystem-->>hexdump: edid bytes
        hexdump-->>QProcess: hex dump output

        CommonTools->>QProcess: waitForFinished(-1)
        alt waitForFinished returns false
            CommonTools-->>CommonTools: qCritical Failed to hexdump edid file
            CommonTools-->>CommonTools: continue to next edid
        else waitForFinished returns true
            CommonTools->>QProcess: readAllStandardOutput()
            QProcess-->>CommonTools: rawLocal8BitData
            CommonTools-->>CommonTools: deviceInfo = QString_fromLocal8Bit(rawLocal8BitData)

            alt deviceInfo is empty
                CommonTools-->>CommonTools: qWarning edid file is empty
                CommonTools-->>CommonTools: continue to next edid
            else deviceInfo is nonempty
                CommonTools-->>CommonTools: split deviceInfo into lines
                CommonTools-->>CommonTools: parse lines for EDID data
            end
        end
    end
Loading

Class diagram for CommonTools_parseEDID and QProcess interaction

classDiagram
    class CommonTools {
        +void parseEDID(QStringList allEDIDS, QString input, bool isHW)
    }

    class QProcess {
        +QProcess()
        +void start(QString program, QStringList arguments)
        +bool waitForFinished(int msecs)
        +QByteArray readAllStandardOutput()
    }

    class QString {
        +static QString fromLocal8Bit(QByteArray byteArray)
    }

    class Logger {
        +static void qCritical(QString message)
        +static void qWarning(QString message)
    }

    CommonTools --> QProcess : uses
    CommonTools --> QString : decodesOutput
    CommonTools --> Logger : logsErrorsAndWarnings
Loading

File-Level Changes

Change Details Files
Use QProcess argument-based start() instead of shell command strings for hexdump execution.
  • Replace string-concatenated hexdump invocations with start("hexdump", QStringList() << args).
  • Pass the EDID path directly as an argument instead of embedding it in a shell command string.
deepin-devicemanager/src/Tool/commontools.cpp
Improve robustness of process execution and output handling in EDID parsing.
  • Check the return value of waitForFinished(-1) and log a critical message and skip processing on failure.
  • Decode process standard output using QString::fromLocal8Bit() before further processing.
  • Detect empty hexdump output, log a warning containing the EDID path, and skip further processing when output is empty.
deepin-devicemanager/src/Tool/commontools.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

@github-actions
Copy link

  • 敏感词检查失败, 检测到1个文件存在敏感词
详情
{
    "deepin-devicemanager/src/Tool/commontools.cpp": [
        {
            "line": "        return \"https://driver.uniontech.com/api/v1/drive/search\";",
            "line_number": 165,
            "rule": "S35",
            "reason": "Url link | 162cef9d4a"
        },
        {
            "line": "        return \"https://driver.uniontech.com/api/v1/drive/search\";",
            "line_number": 169,
            "rule": "S35",
            "reason": "Url link | 162cef9d4a"
        },
        {
            "line": "        return \"https://drive-pre.uniontech.com/api/v1/drive/search\";",
            "line_number": 171,
            "rule": "S35",
            "reason": "Url link | 0e010283c1"
        }
    ]
}

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 - I've reviewed your changes and they look great!


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.

- Refactored QProcess::start() calls in parseEDID() to use the modern
  API by separating program name and arguments instead of shell string concatenation.
- Added return value validation for waitForFinished() to
  handle process execution failures gracefully.
- Applied QString::fromLocal8Bit() to standard output reading to
  ensure proper text encoding handling.
- These changes improve code robustness, security (avoiding shell injection risks),
  and compatibility with Qt6 best practices.

Log: fix issue
Bug: https://pms.uniontech.com/bug-view-353603.html
@GongHeng2017 GongHeng2017 force-pushed the 202603201954-dev-eagle-fix branch from 0c00178 to f54df7c Compare March 20, 2026 13:10
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改主要针对 CommonTools::parseEDID 函数中调用 hexdump 命令的部分。以下是详细的代码审查意见:

1. 语法与逻辑

  • 改进点:参数分离与编码转换

    • 修改前process.start(QString("hexdump %1").arg(edid));
      • 使用字符串拼接构造命令。如果 edid 变量包含空格或特殊字符,可能会导致命令注入风险或参数解析错误。
    • 修改后process.start("hexdump", QStringList() << edid);
      • 评价:这是正确的做法。使用 QStringList 传递参数,QProcess 会自动处理参数的引用和转义,有效防止了命令注入漏洞,逻辑更健壮。
    • 修改前QString deviceInfo = process.readAllStandardOutput();
    • 修改后QString deviceInfo = QString::fromLocal8Bit(process.readAllStandardOutput());
      • 评价:非常必要的改进。readAllStandardOutput() 返回的是 QByteArray,直接赋值给 QString 可能导致乱码(尤其是在非 UTF-8 环境下)。显式使用 fromLocal8Bit 可以确保根据本地编码正确转换文本。
  • 改进点:超时处理

    • 修改前process.waitForFinished(-1);
      • 设置为 -1 表示无限期等待。如果 hexdump 进程卡死,主线程也会随之阻塞挂起,导致界面无响应(ANR)。
    • 修改后if (!process.waitForFinished(3000)) { ... }
      • 评价:增加了 3 秒(3000毫秒)的超时限制。这是一个很好的健壮性改进,防止了因外部命令执行失败导致的程序假死。
  • 改进点:错误日志

    • 评价:增加了 qCriticalqWarning 输出,这对于调试和现场问题排查非常有帮助。

2. 代码质量

  • 重复代码 (DRY原则)

    • 问题if (isHW)else 分支中的代码逻辑高度相似(启动进程 -> 等待 -> 读取输出 -> 检查空值)。唯一的区别是 hexdump 的参数(一个不带 -C,一个带 -C)。
    • 建议:可以将这段逻辑提取为一个私有辅助函数,例如 runHexDump(const QString &filePath, const QStringList &args),以减少代码冗余,提高可维护性。
  • 日志级别

    • 建议qCritical() << "Failed to hexdump edid file!"; 中使用了 Critical 级别。考虑到代码中使用了 continue(即跳过当前文件继续处理下一个),这可能属于非致命错误。使用 qWarning() 可能更合适,除非 hexdump 的失败意味着整个程序功能无法继续。保留 Critical 用于程序无法继续运行的场景。

3. 代码性能

  • 进程启动开销
    • QProcess 的启动和销毁是有开销的。如果 allEDIDS 列表非常庞大,循环中频繁启动外部进程可能会造成性能瓶颈。
    • 建议:如果性能测试发现此处是瓶颈,可以考虑使用 C/C++ 库直接读取并解析二进制 EDID 文件,完全替代调用外部 hexdump 命令。这会显著提升性能并减少对系统 shell 的依赖。

4. 代码安全

  • 命令注入防护
    • 评价:如前所述,将 process.start(QString(...)) 改为 process.start("cmd", args) 是最关键的安全修复。这彻底解决了命令注入的风险,无论 edid 路径名中包含什么字符,都会被安全地作为单一参数传递。

总结

这次修改显著提升了代码的健壮性(超时控制)和安全性(参数分离),并改善了兼容性(字符编码处理)。

进一步优化建议示例代码:

为了消除重复代码,建议重构如下:

// 在 CommonTools 类中添加私有辅助函数声明
// QString readHexDumpOutput(const QString &filePath, const QStringList &extraArgs);

QString CommonTools::readHexDumpOutput(const QString &filePath, const QStringList &extraArgs) {
    QProcess process;
    QStringList args;
    args << extraArgs << filePath;
    
    process.start("hexdump", args);
    if (!process.waitForFinished(3000)) {
        qWarning() << "Hexdump timed out or failed for file:" << filePath;
        process.kill();
        return QString();
    }

    QByteArray output = process.readAllStandardOutput();
    if (output.isEmpty()) {
        qWarning() << "Hexdump output is empty for file:" << filePath;
        return QString();
    }

    return QString::fromLocal8Bit(output);
}

// 在 parseEDID 中调用
void CommonTools::parseEDID(...) {
    // ...
    for (auto edid : allEDIDS) {
        // ...
        QString deviceInfo;
        if (isHW) {
            deviceInfo = readHexDumpOutput(edid, QStringList()); // 不带 -C
        } else {
            deviceInfo = readHexDumpOutput(edid, QStringList() << "-C"); // 带 -C
        }

        if (deviceInfo.isEmpty()) {
            continue;
        }
        
        // 后续解析逻辑...
    }
}

@github-actions
Copy link

  • 敏感词检查失败, 检测到1个文件存在敏感词
详情
{
    "deepin-devicemanager/src/Tool/commontools.cpp": [
        {
            "line": "        return \"https://driver.uniontech.com/api/v1/drive/search\";",
            "line_number": 165,
            "rule": "S35",
            "reason": "Url link | 162cef9d4a"
        },
        {
            "line": "        return \"https://driver.uniontech.com/api/v1/drive/search\";",
            "line_number": 169,
            "rule": "S35",
            "reason": "Url link | 162cef9d4a"
        },
        {
            "line": "        return \"https://drive-pre.uniontech.com/api/v1/drive/search\";",
            "line_number": 171,
            "rule": "S35",
            "reason": "Url link | 0e010283c1"
        }
    ]
}

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017, max-lvs

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

@GongHeng2017
Copy link
Contributor Author

/merge

@GongHeng2017
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Mar 23, 2026

This pr cannot be merged! (status: unstable)

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Mar 23, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 894f7e2 into linuxdeepin:develop/eagle Mar 23, 2026
18 of 22 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