Merged
fix(pyacl): 校验 malloc_with_cfg 属性整数范围 #6297
KaranocaVeAdd on 13 days ago
fix(pyacl): 校验 malloc_with_cfg 属性整数范围 #6297
Merged
Total: 1 files changed+6-3
| @@ -322,12 +322,16 @@ static bool GetUnionValueFromPyDict(PyObject* pyDict, const char* keyName, aclrt | |||
| 322 | 322 | ||
| 323 | if (PyDict_GetItemString(pyValue, "moduleId") != nullptr) { | 323 | if (PyDict_GetItemString(pyValue, "moduleId") != nullptr) { |
| 324 | PyObject* pyModuleId = PyDict_GetItemString(pyValue, "moduleId"); | 324 | PyObject* pyModuleId = PyDict_GetItemString(pyValue, "moduleId"); |
| 325 | - value.moduleId = static_cast<uint16_t>(PyLong_AsUnsignedLong(pyModuleId)); | 325 | + unsigned long moduleIdVal = PyLong_AsUnsignedLong(pyModuleId); |
| 326 | CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); | 326 | CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); |
| 327 | + CHECK_BOOL(moduleIdVal <= UINT16_MAX, "moduleId exceeds uint16 range", PyExc_ValueError); | ||
| 328 | + value.moduleId = static_cast<uint16_t>(moduleIdVal); | ||
| 327 | } else if (PyDict_GetItemString(pyValue, "deviceId") != nullptr) { | 329 | } else if (PyDict_GetItemString(pyValue, "deviceId") != nullptr) { |
| 328 | PyObject* pyDeviceId = PyDict_GetItemString(pyValue, "deviceId"); | 330 | PyObject* pyDeviceId = PyDict_GetItemString(pyValue, "deviceId"); |
| 329 | - value.deviceId = static_cast<uint32_t>(PyLong_AsUnsignedLong(pyDeviceId)); | 331 | + unsigned long deviceIdVal = PyLong_AsUnsignedLong(pyDeviceId); |
| 330 | CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); | 332 | CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); |
| 333 | + CHECK_BOOL(deviceIdVal <= UINT32_MAX, "deviceId exceeds uint32 range", PyExc_ValueError); | ||
| 334 | + value.deviceId = static_cast<uint32_t>(deviceIdVal); | ||
| 331 | } else if (PyDict_GetItemString(pyValue, "rsv") != nullptr) { | 335 | } else if (PyDict_GetItemString(pyValue, "rsv") != nullptr) { |
| 332 | PyObject* pyRsv = PyDict_GetItemString(pyValue, "rsv"); | 336 | PyObject* pyRsv = PyDict_GetItemString(pyValue, "rsv"); |
| 333 | Py_ssize_t size = PyList_Size(pyRsv); | 337 | Py_ssize_t size = PyList_Size(pyRsv); |
| @@ -335,7 +339,6 @@ static bool GetUnionValueFromPyDict(PyObject* pyDict, const char* keyName, aclrt | |||
| 335 | for (int i = 0; i < loopLimit; ++i) { | 339 | for (int i = 0; i < loopLimit; ++i) { |
| 336 | PyObject* pyItem = PyList_GetItem(pyRsv, i); | 340 | PyObject* pyItem = PyList_GetItem(pyRsv, i); |
| 337 | value.rsv[i] = static_cast<uint8_t>(PyLong_AsUnsignedLong(pyItem)); | 341 | value.rsv[i] = static_cast<uint8_t>(PyLong_AsUnsignedLong(pyItem)); |
| 338 | - CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); | ||
| 339 | } | 342 | } |
| 340 | } else { | 343 | } else { |
| 341 | PyErr_SetString(PyExc_KeyError, "No valid key found in aclrtMallocAttrValue"); | 344 | PyErr_SetString(PyExc_KeyError, "No valid key found in aclrtMallocAttrValue"); |
runtime 仓库的结构文档明确说明其为“预留参数。当前固定配置为0”;当前 Device 内存分配实现对 RSV 属性直接跳过,不读取其内容。因此,正常使用 RSV 属性时,其元素截断不会改变当前的内存分配行为。建议rsv 相关校验暂不作为本次合入。