diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 7c0ac1a57f534c..57850c34534108 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; } @@ -2377,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); @@ -2673,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)) { @@ -5649,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; 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);