Merged
修复 `set_attr_list_string` 的悬空字符串指针 #5699
修复 `set_attr_list_string` 的悬空字符串指针 #5699
Merged
KaranocaVeAdd on Oct 21
KaranocaVe
Oct 21

修复 set_attr_list_string 的悬空字符串指针

acl.op.set_attr_list_string 转换 Python 字符串列表时,将 std::vector<std::string> 元素的 data() 地址保存到指针数组。后续向 vector 追加元素触发扩容后,短字符串的旧地址失效;C API 随后读取这些地址时可能访问已释放内存。
独立 AddressSanitizer host harness 使用短字符串复现了悬空指针。修复在转换前按列表长度创建字符串槽位,并在循环中按索引写入,再将各字符串的 data() 地址传给 C API,避免 vector 扩容使地址失效。

关联 Issue: https://gitcode.com/cann/runtime/issues/1172

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for KaranocaVe's contribution.)
KaranocaVeKaranocaVe
Oct 21 Created pull request, commit 06931ac6
KaranocaVeKaranocaVe
Oct 21 Associated issue: 修复 PyACL 字符串列表转换中的悬空指针
atomgit-bot
atomgit-bot
Oct 21 Comments:

变更摘要

本 PR 修复 acl.op.set_attr_list_string 在将 Python 字符串列表转换为 C 参数时的悬空字符串指针问题。原实现把 std::vector<std::string> 中各元素的 data() 地址存入指针数组,后续向 vector 追加元素触发扩容后,短字符串的旧地址失效,C API 读取这些地址时可能访问已释放内存(AddressSanitizer 报告堆 use-after-free)。改动在 WrapAclOpSetAttrListString 中按列表长度预先预留字符串 vector 容量,使已有元素地址在遍历过程中保持稳定,从而消除悬空指针;接口与 C API 均保持不变。

主要改动

  • 预分配字符串 vector 容量: 在 WrapAclOpSetAttrListString 中新增 fileNameArray.reserve(static_cast<size_t>(size)),在进入循环前按列表长度预留容量,避免后续 push_back 触发扩容。
  • 消除短字符串悬空指针: 由于容量已预留,循环中保存到指针数组的 data() 地址不再因扩容而失效,修复了 C API 随后读取这些地址时的堆 use-after-free。
  • 保持接口与 C API 不变: 改动仅调整包装器内部的字符串存储策略,未改变函数签名、调用方式及底层 C API 行为。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Oct 21 Comments:

代码审查

✅ 未发现问题


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Oct 21 Added label: cann-cla/yes
107 messages are collapsed here。 See more
ykl999
ykl999Collaborators
24 days ago Comments:

/approve
/lgtm

likedislike
CANN-robotCANN-robotCollaborators
24 days ago Added label: approved
zhangpengpeng8
zhangpengpeng8Collaborators
24 days ago Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
24 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
24 days ago Merged pull request