Merged
fix(runtime): 校验 PyACL 字典参数的 int/short 范围 #5715
fix(runtime): 校验 PyACL 字典参数的 int/short 范围 #5715
Merged
KaranocaVeAdd on Oct 21
KaranocaVe
Oct 21

fix(runtime): 校验 PyACL 字典参数的 int/short 范围

问题

pyACL 字典参数解析将超出 int/short 范围的 Python 整数静默窄化,导致传入配置与底层实际配置不一致。

修复

在通用转换 helper 中保留宽类型中间值并检查目标类型范围,超范围统一返回 ValueError。

验证

  • 源码审计确认原实现分别使用 static_cast<int>(PyLong_AsLong(...)) 和 static_cast<short>(PyLong_AsLong(...)),窄化发生在错误检查前。
  • 补丁加入目标类型范围检查;git diff --check 通过。
  • 基于上游 master 的 93b7ec22 运行目标仓库 pre-commit(clang-format v16.0.0、OAT),python/pyACL/utils_methods.cpp 检查通过。

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

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for KaranocaVe's contribution.)
KaranocaVeKaranocaVe
Oct 21 Created pull request, commit 4a07b0d5
KaranocaVeKaranocaVe
Oct 21 Associated issue: PyACL 字典标量转换静默窄化
atomgit-bot
atomgit-bot
Oct 21 Comments:

变更摘要

本 PR 针对 python/pyACL/utils_methods.cpp 中 GetValueFromPyDict 的字典参数解析逻辑,修复 Python 整数被静默窄化的问题。原实现分别以 static_cast<int>(PyLong_AsLong(...)) 和 static_cast<short>(PyLong_AsLong(...)) 直接完成转换,窄化发生在范围校验之前;补丁改为先保留 long 宽类型中间值,再校验目标类型范围,超范围统一设置 PyErr_SetString 并返回 false。

主要改动

  • int 重载保留宽类型中间值:将 int tmp = static_cast<int>(PyLong_AsLong(pyObj)) 改为 long tmp = PyLong_AsLong(pyObj),避免在 PyLong_AsLong 成功返回后立即发生窄化。
  • 新增 int 范围校验:在转换前判断 tmp < INT_MIN || tmp > INT_MAX,超范围时设置 PyExc_ValueError("value exceeds int range")并返回 false,赋值改为 static_cast<int>(tmp)。
  • short 重载保留宽类型中间值:将 short tmp = static_cast<short>(PyLong_AsLong(pyObj)) 改为 long tmp = PyLong_AsLong(pyObj),与 int 重载保持一致。
  • 新增 short 范围校验:判断 tmp < SHRT_MIN || tmp > SHRT_MAX,超范围时设置 PyExc_ValueError("value exceeds short range")并返回 false,赋值改为 static_cast<short>(tmp)。

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

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: 8b9309626dbd48910f09be7d007951cf6b50866a)
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。麻烦 @Reyn52166 @wangtao43 @tingwood @ykl999 @houyanbao 抽空协助审批,并在条件满足后推进合并,谢谢!

likedislike
KaranocaVe
KaranocaVe
Oct 21 Comments:

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

likedislike
KaranocaVe
KaranocaVe
Oct 23 Comments:

/compile

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

再次提醒:根据 CANN-robot 当前审批进度,该 PR 仍缺少必要的代码检视或合入审批。麻烦 houyanbao tingwood zhangpengpeng8 yanmingxiang Reyn52166 wangtao43 ykl999 抽空协助审核;确认无误后请按仓库规则执行 /lgtm 和 /approve,谢谢!

likedislike
KaranocaVe
KaranocaVe
28 days ago Comments:

再次提醒:根据 CANN-robot 当前审批进度,该 PR 仍缺少必要的代码检视或合入审批。麻烦 @houyanbao @tingwood @zhangpengpeng8 @yanmingxiang @Reyn52166 @wangtao43 @ykl999 抽空协助审核;确认无误后请按仓库规则执行 /lgtm 和 /approve,谢谢!

likedislike
KaranocaVe
KaranocaVe
25 days ago Comments:

当前 PR 代码审查、CLA、CI 与冲突检查均已通过;机器人状态仅剩合入权限限制(当前账号无目标分支 PUSH 权限,直接合入返回 HTTP 405)。请具备仓库权限的维护者按仓库规则执行合入。

likedislike
ykl999
ykl999Collaborators
24 days ago Comments:

/compile

likedislike
KaranocaVeKaranocaVe
24 days ago Pre merge succeeded (commit_id: 47bcc4d25a879e21c308eaa77d4e6503166c2d66)
ykl999
ykl999Collaborators
24 days ago Comments:

/approve
/lgtm

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

/lgtm

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