https://github.com/python/cpython/commit/7d3d213fccd6cce02a2a3c38e2af7d064a9536ef commit: 7d3d213fccd6cce02a2a3c38e2af7d064a9536ef branch: 3.14 author: Miss Islington (bot) <[email protected]> committer: JelleZijlstra <[email protected]> date: 2026-10-22T15:22:05-07:00 summary:
[3.14] gh-159739: Preserve ctypes base metadata when cloning fails (GH-160174) (#160294) When creating a ctypes structure subclass, cloning its layout initially copied pointers to buffers owned by the base class. If a later allocation failed, cleaning up the subclass could free those buffers, leaving the base with invalid metadata and potentially causing a crash. Detach the owned pointers until their copies have been allocated. Backport to 3.15 and 3.14. (cherry picked from commit 7c873199d4981b787e5c313c8e6036f45d22a06a) Co-authored-by: Jelle Zijlstra <[email protected]> files: A Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst M Modules/_ctypes/stgdict.c diff --git a/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst b/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst new file mode 100644 index 00000000000000..a3682d0cea74dc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst @@ -0,0 +1,2 @@ +Fix a crash when a :mod:`ctypes` structure subclass cannot be created due +to an allocation failure. diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index ab955a0b824a2f..fc47844104876e 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -28,6 +28,10 @@ PyCStgInfo_clone(StgInfo *dst_info, StgInfo *src_info) ctype_free_stginfo_members(dst_info); memcpy(dst_info, src_info, sizeof(StgInfo)); + /* Owned buffers must not be shared if a subsequent allocation fails. */ + dst_info->format = NULL; + dst_info->shape = NULL; + dst_info->ffi_type_pointer.elements = NULL; #ifdef Py_GIL_DISABLED dst_info->mutex = (PyMutex){0}; #endif _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
