]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-151126: Fix `PyErr_NoMemory` errors in `_ctypes.c` (#152720)
authorstevens <lipengyu@kylinos.cn>
Tue, 14 Jul 2026 16:48:17 +0000 (00:48 +0800)
committerGitHub <noreply@github.com>
Tue, 14 Jul 2026 16:48:17 +0000 (22:18 +0530)
Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst [new file with mode: 0644]
Modules/_ctypes/_ctypes.c

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 (file)
index 0000000..d5ec1d2
--- /dev/null
@@ -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.
index e891249668c20f581910ae2edb6c6f880af7c19a..adfdf44e53604e3b57f4763a81012f559d3def78 100644 (file)
@@ -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);