diff --git a/src/kernels/tbe_adapter/platform/platform_ascendc.cpp b/src/kernels/tbe_adapter/platform/platform_ascendc.cpp index e1d924f2cdce43ece07b9efa32d71561ed0807ab..3a1cb87219de3a5c3cbe4a907e685f72baef9be9 100644 --- a/src/kernels/tbe_adapter/platform/platform_ascendc.cpp +++ b/src/kernels/tbe_adapter/platform/platform_ascendc.cpp @@ -41,6 +41,25 @@ const static std::map CONVERT_MAP = { {"Ascend910_93", SocVersion::ASCEND910B}, }; +namespace { +constexpr uint32_t DECIMAL = 10; +uint32_t safeChangeStringtoUnit32(const std::string &str) +{ + if (str.empty()) { + MKI_LOG(WARN) << "var is empty"; + return 0; + } + char *endptr = nullptr; + const char* cstr = str.c_str(); + long result = std::strtol(cstr, &endptr, DECIMAL); + if (cstr == endptr || *endptr != '\0') { + MKI_LOG(WARN) << "Failed to convert string to uint32: " << str; + return 0; + } + return static_cast(result); +} +} + static inline uint32_t GetCoreNumByType(fe::PlatFormInfos *platformInfo, bool isAiv) { std::string key; @@ -57,7 +76,7 @@ static inline uint32_t GetCoreNumByType(fe::PlatFormInfos *platformInfo, bool is } ret = platformInfo->GetPlatformResWithLock(STR_SOC_INFO, key, val); MKI_LOG_IF(!ret, ERROR) << "get platform failed, key is " << key << ", val is" << val; - return val.empty() ? 0 : static_cast(std::atoi(val.c_str())); + return safeChangeStringtoUnit32(val); } uint32_t PlatformAscendC::GetCoreNumVector(void) const @@ -66,7 +85,7 @@ uint32_t PlatformAscendC::GetCoreNumVector(void) const std::string val; bool ret = GetPlatFormInfo()->GetPlatformResWithLock(STR_SOC_INFO, STR_CORE_CNT_VEC, val); MKI_LOG_IF(!ret, ERROR) << "get platform vector num failed, val is " << val; - return val.empty() ? 0 : std::atoi(val.c_str()); + return safeChangeStringtoUnit32(val); } return 0; }