diff --git a/Misc/NEWS b/Misc/NEWS index 7880adb2b70..8c23a9e56bb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -52,6 +52,9 @@ Core and Builtins Library ------- +- Issue #18556: Check the return value of a PyUnicode_AsWideChar() call in + ctypes' U_set(). + - Issue #18549: Eliminate dead code in socket_ntohl() - Issue #18514: Fix unreachable Py_DECREF() call in PyCData_FromBaseObj() diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index f6f8e4236b1..65772cfa45a 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1260,7 +1260,11 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length) } else if (size < length-1) /* copy terminating NUL character if there is space */ size += 1; - PyUnicode_AsWideChar(value, (wchar_t *)ptr, size); + + if (PyUnicode_AsWideChar(value, (wchar_t *)ptr, size) == -1) { + return NULL; + } + return value; }