Merged
修复 `set_attr_list_string` 的悬空字符串指针 #5699
KaranocaVeAdd on Oct 21
修复 `set_attr_list_string` 的悬空字符串指针 #5699
Merged
Total: 2 files changed+40-2
| @@ -373,6 +373,7 @@ PyObject* WrapAclOpSetAttrListString(PyObject* /* self */, PyObject* args) | |||
| 373 | int size = static_cast<int>(PyList_Size(pyValuesList)); | 373 | int size = static_cast<int>(PyList_Size(pyValuesList)); |
| 374 | CHECK_NULL(size >= 0, "list is empty", PyExc_ValueError); | 374 | CHECK_NULL(size >= 0, "list is empty", PyExc_ValueError); |
| 375 | std::vector<std::string> fileNameArray; | 375 | std::vector<std::string> fileNameArray; |
| 376 | + fileNameArray.assign(static_cast<size_t>(size), std::string()); | ||
| 376 | std::vector<const char*> values(size + 1, nullptr); | 377 | std::vector<const char*> values(size + 1, nullptr); |
| 377 | for (int i = 0; i < size; i++) { | 378 | for (int i = 0; i < size; i++) { |
| 378 | PyObject* object = PyList_GetItem(pyValuesList, i); | 379 | PyObject* object = PyList_GetItem(pyValuesList, i); |
| @@ -384,7 +385,7 @@ PyObject* WrapAclOpSetAttrListString(PyObject* /* self */, PyObject* args) | |||
| 384 | PyErr_SetString(PyExc_RuntimeError, "PyBytes_AsString failed"); | 385 | PyErr_SetString(PyExc_RuntimeError, "PyBytes_AsString failed"); |
| 385 | return nullptr; | 386 | return nullptr; |
| 386 | } | 387 | } |
| 387 | - fileNameArray.push_back(str); | 388 | + fileNameArray[i] = str; |
| 388 | Py_XDECREF(bytes); | 389 | Py_XDECREF(bytes); |
| 389 | values[i] = fileNameArray[i].data(); | 390 | values[i] = fileNameArray[i].data(); |
| 390 | } | 391 | } |
| @@ -743,4 +744,4 @@ PyObject* WrapAclOpSetMaxOpQueueNum(PyObject* /* self */, PyObject* args) | |||
| 743 | 744 | ||
| 744 | aclError ret = aclopSetMaxOpQueueNum(maxOpNum); | 745 | aclError ret = aclopSetMaxOpQueueNum(maxOpNum); |
| 745 | return Py_BuildValue("i", ret); | 746 | return Py_BuildValue("i", ret); |
| 746 | -} | 747 | +} |
| @@ -0,0 +1,37 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software; you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include <cassert> | ||
| 12 | +#include <cstring> | ||
| 13 | +#include <string> | ||
| 14 | +#include <vector> | ||
| 15 | + | ||
| 16 | +static int SetAttrListStringStub(int count, const char* const values[]) | ||
| 17 | +{ | ||
| 18 | + assert(count == 2); | ||
| 19 | + assert(std::strcmp(values[0], "a") == 0); | ||
| 20 | + assert(std::strcmp(values[1], "b") == 0); | ||
| 21 | + return 0; | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +int main() | ||
| 25 | +{ | ||
| 26 | + const std::vector<std::string> input = {"a", "b"}; | ||
| 27 | + std::vector<std::string> storage; | ||
| 28 | + storage.assign(input.size(), std::string()); | ||
| 29 | + std::vector<const char*> values(input.size() + 1, nullptr); | ||
| 30 | + | ||
| 31 | + for (size_t i = 0; i < input.size(); ++i) { | ||
| 32 | + storage[i] = input[i]; | ||
| 33 | + values[i] = storage[i].data(); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + return SetAttrListStringStub(static_cast<int>(input.size()), values.data()); | ||
| 37 | +} | ||