From: stevens Date: Tue, 14 Jul 2026 16:48:17 +0000 (+0800) Subject: gh-151126: Fix `PyErr_NoMemory` errors in `_ctypes.c` (#152720) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d807210fee5606f8d504c1677f7e5ecb5f208a1e;p=thirdparty%2FPython%2Fcpython.git gh-151126: Fix `PyErr_NoMemory` errors in `_ctypes.c` (#152720) --- diff --git a/Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst b/Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst new file mode 100644 index 000000000000..d5ec1d254ef0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst @@ -0,0 +1,3 @@ +Fix a crash caused by failing to set :exc:`MemoryError` on allocation failure +when passing :class:`ctypes.Structure` or :class:`ctypes.Union` instances by +value to ctypes foreign functions. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index e891249668c2..adfdf44e5360 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -673,6 +673,7 @@ StructUnionType_paramfunc(ctypes_state *st, CDataObject *self) if ((size_t)self->b_size > sizeof(void*)) { ptr = PyMem_Malloc(self->b_size); if (ptr == NULL) { + PyErr_NoMemory(); return NULL; } memcpy(ptr, self->b_ptr, self->b_size);