已合并
[security]fix 'new' and 'stoi' #26123
SCh_zx创建于 2025年10月29日
[security]fix 'new' and 'stoi' #26123
已合并
SCh_zx创建于 2025年10月29日
5 个文件变更+27-9
@@ -74,7 +74,7 @@ class TestScheduleContext(TestCase):
74 {"schedule_mode": 1, "attention_window_size": 511},74 {"schedule_mode": 1, "attention_window_size": 511},
75 "attention_window_size is not enough should fail",75 "attention_window_size is not enough should fail",
76 ),76 ),
77- ({"schedule_mode": 2}, "schedule_mode 2 is not supportted"),77+ ({"schedule_mode": 2}, "schedule_mode 2 is not supported"),
78 (78 (
79 {"attn_to_ffn_token_size": 1023},79 {"attn_to_ffn_token_size": 1023},
80 "attn_to_ffn_token_size must be aligned by 512",80 "attn_to_ffn_token_size must be aligned by 512",
@@ -15,7 +15,7 @@ at::Tensor NPUNativeFunctions::contiguous(const at::Tensor& self, c10::MemoryFor
15 15 
16 TORCH_CHECK(16 TORCH_CHECK(
17 memory_format == c10::MemoryFormat::Contiguous,17 memory_format == c10::MemoryFormat::Contiguous,
18- "NPU contiguous operator only supportted contiguous memory format.", OPS_ERROR(ErrCode::NOT_SUPPORT));18+ "NPU contiguous operator only supported contiguous memory format.", OPS_ERROR(ErrCode::NOT_SUPPORT));
19 return self.clone(memory_format);19 return self.clone(memory_format);
20}20}
21 21 
@@ -1,3 +1,4 @@
1+#include <climits>
1#include <unordered_map>2#include <unordered_map>
2#include "torch_npu/csrc/core/npu/interface/DcmiInterface.h"3#include "torch_npu/csrc/core/npu/interface/DcmiInterface.h"
3#include "torch_npu/csrc/core/npu/NPUException.h"4#include "torch_npu/csrc/core/npu/NPUException.h"
@@ -47,10 +48,19 @@ c10_npu::CoreIdRange parseAffinityCPU(const std::string cpuString)
47 if (pos != std::string::npos) {48 if (pos != std::string::npos) {
48 std::string start = cpuString.substr(0, pos);49 std::string start = cpuString.substr(0, pos);
49 std::string end = cpuString.substr(pos + 1);50 std::string end = cpuString.substr(pos + 1);
50- int startNum = stoi(start);51+ 
51- int endNum = stoi(end);52+ char* start_endptr = nullptr;
52- if (startNum < endNum) {53+ char* end_endptr = nullptr;
53- return c10_npu::CoreIdRange{startNum, endNum};54+ 
55+ errno = 0;
56+ long startNum = strtol(start.c_str(), &start_endptr, 10);
57+ long endNum = strtol(end.c_str(), &end_endptr, 10);
58+ if (start_endptr != start.c_str() && *start_endptr == '\0' &&
59+ end_endptr != end.c_str() && *end_endptr == '\0' &&
60+ startNum >= 0 && endNum >= 0 && startNum <= INT_MAX && endNum <= INT_MAX &&
61+ errno != ERANGE &&
62+ startNum < endNum) {
63+ return c10_npu::CoreIdRange{static_cast<int>(startNum), static_cast<int>(endNum)};
54 }64 }
55 }65 }
56 TORCH_CHECK(false, "affinity cpu " + cpuString + " is error ", PTA_ERROR(ErrCode::VALUE));66 TORCH_CHECK(false, "affinity cpu " + cpuString + " is error ", PTA_ERROR(ErrCode::VALUE));
@@ -2315,14 +2315,22 @@ private:
2315 if (IsMallocPage1GMem(pool->is_small)) {2315 if (IsMallocPage1GMem(pool->is_small)) {
2316 segment_size = kExtraLargeBuffer;2316 segment_size = kExtraLargeBuffer;
2317 }2317 }
2318- auto segment = new ExpandableSegment(device, stream, segment_size);2318+ auto segment = new (std::nothrow) ExpandableSegment(device, stream, segment_size);
2319+ if (!segment) {
2320+ ASCEND_LOGE("Failed to allocate ExpandableSegment.");
2321+ return nullptr;
2322+ }
2319 if (hcclComm_) {2323 if (hcclComm_) {
2320 segment->setHcclComm(hcclComm_);2324 segment->setHcclComm(hcclComm_);
2321 }2325 }
2322 expandable_segments_.emplace_back(segment);2326 expandable_segments_.emplace_back(segment);
2323 2327 
2324 ExpandableSegment *es = expandable_segments_.back();2328 ExpandableSegment *es = expandable_segments_.back();
2325- Block *candidate = new Block(device, stream, es->size(), pool, es->ptr());2329+ Block *candidate = new (std::nothrow) Block(device, stream, es->size(), pool, es->ptr());
2330+ if (!candidate) {
2331+ ASCEND_LOGE("Failed to allocate Block.");
2332+ return nullptr;
2333+ }
2326 candidate->mapped = false;2334 candidate->mapped = false;
2327 candidate->expandable_segment_ = es;2335 candidate->expandable_segment_ = es;
2328 pool->unmapped.insert(candidate);2336 pool->unmapped.insert(candidate);
@@ -148,7 +148,7 @@ inline const char* getErrorFunction(const char* /* msg */, const char* args)
148 static auto feature_not_support_warn_once = []() { \148 static auto feature_not_support_warn_once = []() { \
149 printf("[WARN]%s,%s:%u:%s\n", \149 printf("[WARN]%s,%s:%u:%s\n", \
150 __FUNCTION__, __FILE__, __LINE__, \150 __FUNCTION__, __FILE__, __LINE__, \
151- "Feature is not supportted and the possible cause is" \151+ "Feature is not supported and the possible cause is" \
152 " that driver and firmware packages do not match."); \152 " that driver and firmware packages do not match."); \
153 return true; \153 return true; \
154 }(); \154 }(); \