Skip to content

fix: handle failure in resolving executable path for process ID#494

Merged
max-lvs merged 1 commit intolinuxdeepin:release/eaglefrom
LiHua000:release/eagle
Feb 11, 2026
Merged

fix: handle failure in resolving executable path for process ID#494
max-lvs merged 1 commit intolinuxdeepin:release/eaglefrom
LiHua000:release/eagle

Conversation

@LiHua000
Copy link

  • Added error handling for the case when the executable path cannot be resolved from the /proc/{pid}/exe symlink.
  • Log a warning message when the resolution fails, improving debugging capabilities.

Log: Enhance robustness in process ID executable path resolution.

- Added error handling for the case when the executable path cannot be resolved from the /proc/{pid}/exe symlink.
- Log a warning message when the resolution fails, improving debugging capabilities.

Log: Enhance robustness in process ID executable path resolution.
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LiHua000, 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

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码修改主要针对 getProcIdExe 函数中 realpath 的使用进行了改进。以下是对该修改的详细审查意见:

1. 语法逻辑

审查结果:通过

  • 改进点:原代码直接对 realpath 返回的指针 actualpath 进行解引用,未判断是否为 nullptr。在 Linux 中,如果 /proc/{pid}/exe 指向的文件已被删除或进程不存在,realpath 可能会返回 NULL。原代码直接构造 QString(actualpath) 会导致程序崩溃(Segmentation Fault)。
  • 修改后:增加了 if (actualpath) 判断,逻辑更加严密,避免了空指针解引用的风险。

2. 代码质量

审查结果:良好,有优化空间

  • 改进点:增加了错误日志 qWarning(),这对于系统监控类服务排查问题非常有帮助。
  • 建议
    • 内存管理:虽然代码中正确添加了 free(actualpath),但在 C++ 中使用原始指针管理内存容易出错。建议使用 RAII(资源获取即初始化)风格的包装,或者使用 C++ 的 std::unique_ptr 配合自定义删除器,防止在复杂的逻辑分支中忘记释放内存(尽管当前逻辑简单)。
    • 字符串转换exeSymlinkPath.toStdString().c_str() 会产生一个临时的 std::string 对象,虽然在这里没问题,但略显冗余。可以直接使用 exeSymlinkPath.toLocal8Bit().constData(),这在处理文件路径时通常更符合 Qt 的习惯。

3. 代码性能

审查结果:影响极小

  • 分析
    • 增加的 if 判断和日志输出对性能影响微乎其微,因为文件路径解析 (realpath) 本身是系统调用,耗时远大于这几行代码。
    • realpath 涉及磁盘 I/O 和符号链接解析,是主要的性能瓶颈点,但这是功能必须的。

4. 代码安全

审查结果:显著提升

  • 内存泄漏修复:原代码存在严重的内存泄漏。realpath 在传入 nullptr 作为第二个参数时,会由 malloc 分配内存存储解析后的路径,调用者必须负责 free。原代码遗漏了这一步,长期运行会导致内存泄漏。修改版正确添加了 free,修复了安全隐患。
  • 空指针保护:如前所述,修复了潜在的崩溃风险。

综合改进建议代码

结合上述分析,建议代码可以进一步优化如下,利用 Qt 特性简化代码并增强安全性:

#include <QByteArray>
#include <QDebug>

static QString getProcIdExe(qint64 id)
{
    QString execName;
    if (id > 0) {
        // Read contents of virtual /proc/{pid}/exe file
        QString exeSymlinkPath = QString("/proc/%1/exe").arg(id);
        
        // 使用 QByteArray 自动管理内存,避免手动 free
        // 注意:realpath 需要 char*,QByteArray 数据是可写的
        QByteArray buffer(PATH_MAX, 0); // 预分配 PATH_MAX 大小
        
        // 使用 buffer.data() 作为接收缓冲区,避免 malloc/free
        if (realpath(exeSymlinkPath.toLocal8Bit().constData(), buffer.data())) {
            execName = QString(buffer);
        } else {
            // 记录错误,包含 errno 可以更好地诊断原因(如权限不足、文件不存在等)
            qWarning() << "Failed to resolve path for PID" << id << ", error:" << strerror(errno);
        }
    }
    return execName;
}

优化点说明:

  1. 栈上分配内存:使用 QByteArray buffer(PATH_MAX, 0) 在栈上预分配内存,避免了堆内存分配(malloc)和释放(free)的开销,同时也消除了内存泄漏的可能性。
  2. Qt 风格转换:使用 toLocal8Bit() 处理文件路径,比 toStdString() 更轻量且符合 Qt 规范。
  3. 错误信息增强:在日志中加入了 strerror(errno),当路径解析失败时(例如进程已死或权限不足),能提供更具体的错误信息,便于调试。

@LiHua000
Copy link
Author

/merge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 11, 2026

This pr cannot be merged! (status: unstable)

@max-lvs max-lvs merged commit 6e98349 into linuxdeepin:release/eagle Feb 11, 2026
7 of 9 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