Repository navigation
fix: resolve dangling pointer in filePath - #412
deepin-bot[bot] merged 1 commit into
Conversation
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
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThe 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 handlingsequenceDiagram
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)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review🤖 AI 代码审查报告📊 总体评价
🔍 详细分析1. 语法逻辑 ✅评价: 语法正确,逻辑清晰 ✅ 通过 潜在问题: 建议: [] 2. 代码质量 ✅评价: 代码结构清晰,注释完整 ✅ 通过 潜在问题: 建议: [] 3. 代码性能 ✅评价: 性能良好,资源使用合理 ✅ 通过 潜在问题: 建议: [] 4. 代码安全 🔒评价: 存在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 代码审查工具自动生成 |
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
911d448
into
linuxdeepin:develop/meagle-20260526
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:
Enhancements:
Chores: