Merged
fix(runtime): 校验 acl.util.ptr_to_bytes 空指针参数 #5697
fix(runtime): 校验 acl.util.ptr_to_bytes 空指针参数 #5697
Merged
KaranocaVeAdd on Oct 21
KaranocaVe
Oct 21

fix(runtime): 校验 acl.util.ptr_to_bytes 空指针参数

WrapPtrToBytes 将空指针传给 PyBytes_FromStringAndSize。CPython 将该调用解释为创建未初始化内容的 bytes 对象,因此 acl.util.ptr_to_bytes(0, size) 在 size > 0 时会返回内容未定义的对象。
在转换前拒绝“空地址且长度非零”,并在 Python API 文档中补充参数约束。零长度转换仍允许空地址。
使用 Python 3.11 的 CPython C API 做了有效地址和空地址对照;空地址加正长度返回指定长度的 bytes 对象。补丁检查通过;未编译完整扩展或执行设备测试。

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

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for KaranocaVe's contribution.)
KaranocaVeKaranocaVe
Oct 21 Created pull request, commit 1f7051b6
KaranocaVeKaranocaVe
Oct 21 Associated issue: fix(runtime): 校验 acl.util.ptr_to_bytes 空指针参数
atomgit-bot
atomgit-bot
Oct 21 Comments:

变更摘要

本 PR 针对 python/pyACL/util/bytes_convert.cpp 中 WrapPtrToBytes 的函数行为进行修正:在将指针转换为 bytes 对象前,新增对空指针参数的校验。此前空地址会被直接传入 PyBytes_FromStringAndSize,CPython 会据此创建内容未定义的 bytes 对象,导致 acl.util.ptr_to_bytes(0, size) 在 size > 0 时返回内容不确定的对象。本次改动在转换前拒绝「空地址且长度非零」的入参,同时保留零长度转换允许空地址的行为。

主要改动

  • 新增空指针参数校验: 在 WrapPtrToBytes 中解析 args 之后、调用 PyBytes_FromStringAndSize 之前,插入 CHECK_NULL(ptr != nullptr || size == 0, ...),对空地址且长度非零的入参进行拦截。
  • 明确零长度转换的边界条件: 校验条件 size == 0 使得空地址在零长度场景下仍然合法,维持原有零长度转换可用。
  • 统一错误类型为 PyExc_ValueError: 校验失败时以 PyExc_ValueError 上报错误信息 ptr must not be null when size is nonzero,与同函数中后续转换失败的异常类型保持一致。

🧩 本次 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
CANN-robot
CANN-robotCollaborators
Oct 21 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/runtime ✅ 卢煜坤, yanmingxiang (2/2) ✅ 卢煜坤 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

KaranocaVe, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Oct 21 Set wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood as approver
CANN-robotCANN-robotCollaborators
Oct 21 Set wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood as approver
KaranocaVe
KaranocaVe
Oct 21 Comments:

/compile

likedislike
KaranocaVeKaranocaVe
Oct 21 Pre merge succeeded (commit_id: a0f2e07716d5d9b2716d395d9b2f170e14bb008e)
CANN-robot
CANN-robotCollaborators
Oct 21 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Oct 21 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 21 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 21 Added label: ci-pipeline-passed
KaranocaVe
KaranocaVe
Oct 21 Comments:

按机器人当前审批进度,模块 repo-cann/runtime 还缺 approve。麻烦 @yanmingxiang @Reyn52166 @houyanbao @ykl999 @wangtao43 抽空协助审批,并在条件满足后推进合并,谢谢!

likedislike
KaranocaVe
KaranocaVe
Oct 21 Comments:

再跟进一下:按机器人最新审批进度,模块 repo-cann/runtime 仍待 approve。麻烦 @yanmingxiang @Reyn52166 @houyanbao @ykl999 @wangtao43 再次抽空协助审批,并在审批条件满足后推进合并,谢谢!

likedislike
KaranocaVe
KaranocaVe
Oct 23 Comments:

/compile

likedislike
KaranocaVeKaranocaVe
Oct 23 Pre merge succeeded (commit_id: c8e217cb9151a644b73d2255bab8dc303940b6aa)
ykl999
ykl999Collaborators
28 days ago Comments:

/compile

likedislike
KaranocaVeKaranocaVe
28 days ago Pre merge succeeded (commit_id: 1acc66b66870347852166d5b260c55d52a698379)
ykl999
ykl999Collaborators
28 days ago Comments:

/approve
/lgtm

likedislike
CANN-robotCANN-robotCollaborators
28 days ago Added label: approved
CANN-robot
CANN-robotCollaborators
28 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
28 days ago Removed label: ci-pipeline-passed
CANN-robotCANN-robotCollaborators
28 days ago Added label: ci-pipeline-running
yanmingxiang
yanmingxiangCollaborators
28 days ago Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
28 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
28 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
28 days ago Added label: ci-pipeline-passed
CANN-robotCANN-robotCollaborators
28 days ago Merged pull request