merge fix/pyacl-malloc-cfg-attr-truncation-20261019 into master
1 file(s) changed+10-3
Mpython/pyACL/rt/rt_memory.cpp+10-3
| @@ -322,20 +322,27 @@ 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"); |
| 337 | + CHECK_BOOL(PyList_Check(pyRsv), "rsv must be a list", PyExc_TypeError); | ||
| 333 | Py_ssize_t size = PyList_Size(pyRsv); | 338 | Py_ssize_t size = PyList_Size(pyRsv); |
| 334 | int loopLimit = (size < 8) ? static_cast<int>(size) : 8; | 339 | int loopLimit = (size < 8) ? static_cast<int>(size) : 8; |
| 335 | for (int i = 0; i < loopLimit; ++i) { | 340 | for (int i = 0; i < loopLimit; ++i) { |
| 336 | PyObject* pyItem = PyList_GetItem(pyRsv, i); | 341 | PyObject* pyItem = PyList_GetItem(pyRsv, i); |
| 337 | - value.rsv[i] = static_cast<uint8_t>(PyLong_AsUnsignedLong(pyItem)); | 342 | + unsigned long item = PyLong_AsUnsignedLong(pyItem); |
| 338 | CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); | 343 | CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError); |
| 344 | + CHECK_BOOL(item <= UINT8_MAX, "the rsv element exceeds uint8 range", PyExc_ValueError); | ||
| 345 | + value.rsv[i] = static_cast<uint8_t>(item); | ||
| 339 | } | 346 | } |
| 340 | } else { | 347 | } else { |
| 341 | PyErr_SetString(PyExc_KeyError, "No valid key found in aclrtMallocAttrValue"); | 348 | PyErr_SetString(PyExc_KeyError, "No valid key found in aclrtMallocAttrValue"); |
Comments
Sign in to comment