Skip to content

fix: resolve dangling pointer in filePath - #412

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/meagle-20260526from
liyigang1:agent/pms-bug-bot/meagle-20260526
Oct 23, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/meagle-20260526from
liyigang1:agent/pms-bug-bot/meagle-20260526

Conversation

@liyigang1

@liyigang1 liyigang1 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Cherry-pick of PR #411 to develop/meagle-20260526

将 PR #411 的修复提交 cherry-pick 到 develop/meagle-20260526 分支。

原始 PR

#411

根因

DEnumeratorPrivate::filePath() 通过 return path.toUtf8().data() / url.userInfo()...toLatin1().data() 返回 char*,临时 QByteArray 在函数返回时被销毁,导致悬垂指针。sortFileInfoList() 中使用该悬垂指针做 strcmp 触发未定义行为,导致目录遍历返回错误的文件数量。

修复方案

将 filePath() 返回类型从 char* 改为 QByteArray(按值返回),利用 RAII 管理生命周期;openDirByfts() 用 strdup() 包装供 fts_open 使用,sortFileInfoList() 直接使用 constData()。

改动文件

  • src/dfm-io/dfm-io/denumerator.cpp:filePath() 返回 QByteArray;openDirByfts() 用 strdup() + null 检查;sortFileInfoList() 用 QByteArray 管理生命周期
  • src/dfm-io/dfm-io/private/denumerator_p.h:声明改为 QByteArray filePath(const QUrl &url)
  • src/dfm-io/tools/dfm-list.cpp:移除未使用的 #include <stdio.h>

影响范围

filePath() 为 private 方法,无 API/ABI 影响。

PMS: BUG-380499 BUG-333909

Summary by Sourcery

Eliminate dangling pointers in file-path handling to ensure reliable directory enumeration.

Bug Fixes:

  • Prevent directory enumeration errors and undefined behavior caused by dangling file-path pointers.

Enhancements:

  • Manage file-path data with QByteArray ownership and lifetime guarantees across directory opening and sorting.

Chores:

  • Remove an unused standard I/O header.

1. Root cause: DEnumeratorPrivate::filePath() returned
   toUtf8().data()/toLatin1().data() from temporary QByteArray,
   causing dangling pointer after function return
2. Fix: change filePath() to return QByteArray by value, ensuring
   RAII lifetime; openDirByfts() wraps result with strdup() for
   fts_open, sortFileInfoList() uses constData() directly
3. Impact: eliminates dangling pointer and manual free() in
   sortFileInfoList(), fixing incorrect file counts in directory
   traversal; remove unused stdio.h include in dfm-list

Log: Fix incorrect directory traversal file counts caused by dangling pointer in filePath

Influence:
1. Test directory enumeration returns correct file counts
2. Test directory traversal with special characters in paths
3. Verify no memory leaks in filePath and sortFileInfoList

fix: 修复 filePath 悬垂指针导致目录遍历文件计数错误

1. 根因:DEnumeratorPrivate::filePath() 通过 toUtf8().data()/
   toLatin1().data() 返回临时 QByteArray 的内部指针,函数返回
   后临时对象销毁产生悬垂指针
2. 方案:将 filePath() 返回类型改为 QByteArray(按值返回),利用
   RAII 管理生命周期;openDirByfts() 用 strdup() 包装供 fts_open
   使用,sortFileInfoList() 直接使用 constData()
3. 影响:消除 sortFileInfoList() 中的悬垂指针和手动 free(),
   修复目录遍历文件计数不正确的问题

Log: 修复目录遍历文件计数不正确的问题

Influence:
1. 测试目录遍历返回正确的文件数量
2. 测试包含特殊字符路径的目录遍历
3. 验证 filePath 和 sortFileInfoList 无内存泄漏

PMS: BUG-380499 BUG-333909
@sourcery-ai

sourcery-ai Bot commented Oct 23, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR fixes undefined behavior in directory enumeration by changing the private file-path helper to return an owning QByteArray, keeping that buffer alive during sorting, and explicitly duplicating it for fts_open; it also removes an unused include.

Sequence diagram for safe directory path handling

sequenceDiagram
    participant Enumerator as DEnumerator
    participant Helper as DEnumeratorPrivate
    participant FTS as fts

    Enumerator->>Helper: filePath(uri)
    Helper-->>Enumerator: QByteArray
    Enumerator->>FTS: strdup(filePath(uri).constData())
    FTS-->>Enumerator: owned path for fts_open
    Enumerator->>Helper: filePath(uri)
    Helper-->>Enumerator: QByteArray dirPathBa
    Enumerator->>FTS: strcmp(dirPathBa.constData(), ent->fts_path)
    Enumerator->>FTS: fts_close(d->fts)
Loading

File-Level Changes

Change Details Files
Replaced temporary-buffer-derived path pointers with value-owned QByteArray results to eliminate dangling-pointer use.
  • Changed the private path conversion helper to return QByteArray by value for both origin paths and regular paths.
  • Updated sorting to retain the QByteArray for the duration of directory traversal and pass its constData() to strcmp-related logic.
  • Updated fts directory opening to duplicate the QByteArray contents with strdup(), while preserving allocation-failure handling.
src/dfm-io/dfm-io/denumerator.cpp
src/dfm-io/dfm-io/private/denumerator_p.h
Removed an obsolete standard I/O header from the command-line utility.
  • Deleted the unused stdio include.
src/dfm-io/tools/dfm-list.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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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 ✨

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交修复了 denumerator.cpp 中 filePath 函数的悬垂指针问题,将返回类型从 char* 改为 QByteArray, 利用 RAII 机制自动管理内存生命周期,消除了手动 strdup/free 带来的内存管理风险。所有调用方均已正确适配, 语法逻辑正确,代码质量良好,无性能问题,无安全漏洞。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// 当前实现已经是正确的修复方式,无需额外修改。
// filePath() 返回 QByteArray,利用 RAII 管理内存:
//
// QByteArray DEnumeratorPrivate::filePath(const QUrl &url)
// {
//     if (url.userInfo().startsWith("originPath::"))
//         return url.userInfo().replace("originPath::", "").toLatin1();
//     QString path = url.path();
//     if (path != "/" && path.endsWith("/"))
//         path = path.left(path.length() - 1);
//     return path.toUtf8();
// }
//
// 调用方需要可变 char* 时用 strdup 包装:
//   paths[0] = strdup(filePath(uri).constData());
// 调用方只需只读访问时直接用 constData():
//   QByteArray dirPathBa = d->filePath(d->uri);
//   const char *dirPath = dirPathBa.constData();

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@liyigang1

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 911d448 into linuxdeepin:develop/meagle-20260526 Oct 23, 2026
17 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.

4 participants