From d2f339e256480990a3c2880b5e2e37b2cf4fccd5 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Wed, 19 Mar 2025 00:56:27 +0500 Subject: [PATCH 1/5] Visit keep in StructParam_traverse --- Modules/_ctypes/_ctypes.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 7c0ac1a57f534c..d99721099bb7fe 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -409,9 +409,11 @@ typedef struct { #define _StructParamObject_CAST(op) ((StructParamObject *)(op)) static int -StructParam_traverse(PyObject *self, visitproc visit, void *arg) +StructParam_traverse(PyObject *myself, visitproc visit, void *arg) { + StructParamObject *self = _StructParamObject_CAST(myself); Py_VISIT(Py_TYPE(self)); + Py_VISIT(self->keep); return 0; } From 8ff32f738a438ee31d961771991823a398be1cff Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Wed, 19 Mar 2025 00:56:47 +0500 Subject: [PATCH 2/5] Decref swapped in PyCSimpleType_init --- Modules/_ctypes/_ctypes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index d99721099bb7fe..c74d2df97ff5df 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2379,6 +2379,7 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds) } StgInfo *sw_info; if (PyStgInfo_FromType(st, swapped, &sw_info) < 0) { + Py_DECREF(swapped); return -1; } assert(sw_info); From d8d01e17424e445ffa894ab587e20e946c1b4ec8 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Wed, 19 Mar 2025 00:57:03 +0500 Subject: [PATCH 3/5] Decref ob in make_funcptrtype_dict --- Modules/_ctypes/_ctypes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index c74d2df97ff5df..99006ddcf44a7d 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2676,6 +2676,7 @@ make_funcptrtype_dict(ctypes_state *st, PyObject *attrdict, StgInfo *stginfo) if (ob) { StgInfo *info; if (PyStgInfo_FromType(st, ob, &info) < 0) { + Py_DECREF(ob); return -1; } if (ob != Py_None && !info && !PyCallable_Check(ob)) { From 2038b589113067b63bbad5e60a109df227eec38b Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Wed, 19 Mar 2025 00:57:38 +0500 Subject: [PATCH 4/5] Check Pointer_item result while constructing result list in Pointer_subscript --- Modules/_ctypes/_ctypes.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 99006ddcf44a7d..57850c34534108 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5653,6 +5653,10 @@ Pointer_subscript(PyObject *myself, PyObject *item) for (cur = start, i = 0; i < len; cur += step, i++) { PyObject *v = Pointer_item(myself, cur); + if (!v) { + Py_DECREF(np); + return NULL; + } PyList_SET_ITEM(np, i, v); } return np; From f27c11a88ed7a3a7b0e8b7c5033a9add3c154291 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Wed, 19 Mar 2025 01:02:15 +0500 Subject: [PATCH 5/5] Fix align and size naming in PyCStructUnionType_update_stginfo - as discussed in previous PR --- Modules/_ctypes/stgdict.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index 107716d85fed2d..4779d362d6a262 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -225,8 +225,8 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct // They're cleared on error. PyObject *layout_func = NULL; PyObject *kwnames = NULL; - PyObject* align = NULL; - PyObject* size = NULL; + PyObject *align_obj = NULL; + PyObject *size_obj = NULL; PyObject *layout_fields_obj = NULL; PyObject *layout_fields = NULL; PyObject *layout = NULL; @@ -291,12 +291,12 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct goto error; } - align = PyObject_GetAttr(layout, &_Py_ID(align)); - if (!align) { + align_obj = PyObject_GetAttr(layout, &_Py_ID(align)); + if (!align_obj) { goto error; } - Py_ssize_t total_align = PyLong_AsSsize_t(align); - Py_CLEAR(align); + Py_ssize_t total_align = PyLong_AsSsize_t(align_obj); + Py_CLEAR(align_obj); if (total_align < 0) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, @@ -305,12 +305,12 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct goto error; } - size = PyObject_GetAttr(layout, &_Py_ID(size)); - if (!size) { + size_obj = PyObject_GetAttr(layout, &_Py_ID(size)); + if (!size_obj) { goto error; } - Py_ssize_t total_size = PyLong_AsSsize_t(size); - Py_CLEAR(size); + Py_ssize_t total_size = PyLong_AsSsize_t(size_obj); + Py_CLEAR(size_obj); if (total_size < 0) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, @@ -669,8 +669,8 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct error: Py_XDECREF(layout_func); Py_XDECREF(kwnames); - Py_XDECREF(align); - Py_XDECREF(size); + Py_XDECREF(align_obj); + Py_XDECREF(size_obj); Py_XDECREF(layout_fields_obj); Py_XDECREF(layout_fields); Py_XDECREF(layout);