已关闭
[Bug-Report|缺陷反馈]: GetTensorNum 用 -1 作错误码,与合法返回值语义混淆 #104
syy_3597创建于  12 天前关闭于  11 天前
syy_3597
12 天前 创建

Describe the current behavior / 问题描述

core/utils/common_check.cpp:16:

int64_t GetTensorNum(uint64_t dimsNum, int64_t *storageDims)
{
    if (storageDims == nullptr) {
        return -1;     // <- 错误码 -1
    }
    int64_t elenmentNum = 1;
    for (...) { elenmentNum *= storageDims[i]; }
    return elenmentNum;
}

问题:

  1. 返回类型是尺寸语义的 int64_t,却用 -1 兼任错误码,调用方无法从类型上区分"错误"与"合法值";
  2. 若某维为 0,合法返回 0,与"错误"同属非正数,调用方 if (num != expectedNum) 之外的任何数值判断都可能混淆;
  3. 顺带:局部变量名 elenmentNum 拼写错误(应为 elementNum)。

该函数是内部工具函数(未对外暴露),修改封闭在 core/utils 内,无 ABI 兼容负担。

Impact / 影响

语义清晰度问题,当前无直接错误行为(现调用方只做相等比较),但为后续维护埋雷。

Suggested fix / 修复建议

签名改为 std::optional<int64_t> GetTensorNum(...),空指针时返回 std::nullopt;同时修正变量拼写。回归依赖:现有 UT 全量。

Environment / 环境信息

  • 位置: core/utils/common_check.cpp:16
  • 调用方: AsdTensorNumCheck、SwapLast2AxesShapeCheck
likedislike
east_yang成员
12 天前 评论:

感谢您的反馈,当前@east_yang跟踪处理中。

likedislike
Ssyy_3597
12 天前 关联了pull request:fix: 修复代码审查发现的健壮性与文档问题(#96-#105)
CANN-robotCANN-robot成员
11 天前 关闭了 issue
CANN-robotCANN-robot成员
11 天前 添加了label:resolved