8000 gh-131311: Fix additional memory leaks in ctypes (GH-131429) · python/cpython@9c4fb92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c4fb92

Browse files
gh-131311: Fix additional memory leaks in ctypes (GH-131429)
* Visit keep in StructParam_traverse * Decref swapped in PyCSimpleType_init * Decref ob in make_funcptrtype_dict * Check Pointer_item result while constructing result list in Pointer_subscript * Fix align and size naming in PyCStructUnionType_update_stginfo - as discussed in previous PR
1 parent 71ce4ac commit 9c4fb92

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,11 @@ typedef struct {
410410
#define _StructParamObject_CAST(op) ((StructParamObject *)(op))
411411

412412
static int
413-
StructParam_traverse(PyObject *self, visitproc visit, void *arg)
413+
StructParam_traverse(PyObject *myself, visitproc visit, void *arg)
414414
{
415+
StructParamObject *self = _StructParamObject_CAST(myself);
415416
Py_VISIT(Py_TYPE(self));
417+
Py_VISIT(self->keep);
416418
return 0;
417419
}
418420

@@ -2378,6 +2380,7 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
23782380
}
23792381
StgInfo *sw_info;
23802382
if (PyStgInfo_FromType(st, swapped, &sw_info) < 0) {
2383+
Py_DECREF(swapped);
23812384
return -1;
23822385
}
23832386
assert(sw_info);
@@ -2674,6 +2677,7 @@ make_funcptrtype_dict(ctypes_state *st, PyObject *attrdict, StgInfo *stginfo)
26742677
if (ob) {
26752678
StgInfo *info;
26762679
if (PyStgInfo_FromType(st, ob, &info) < 0) {
2680+
Py_DECREF(ob);
26772681
return -1;
26782682
}
26792683
if (ob != Py_None && !info && !PyCallable_Check(ob)) {
@@ -5650,6 +5654,10 @@ Pointer_subscript(PyObject *myself, PyObject *item)
56505654

56515655
for (cur = start, i = 0; i < len; cur += step, i++) {
56525656
PyObject *v = Pointer_item(myself, cur);
5657+
if (!v) {
5658+
Py_DECREF(np);
5659+
return NULL;
5660+
}
56535661
PyList_SET_ITEM(np, i, v);
56545662
}
56555663
return np;

Modules/_ctypes/stgdict.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
225225
// They're cleared on error.
226226
PyObject *layout_func = NULL;
227227
PyObject *kwnames = NULL;
228-
PyObject* align = NULL;
229-
PyObject* size = NULL;
228+
PyObject *align_obj = NULL;
229+
PyObject *size_obj = NULL;
230230
PyObject *layout_fields_obj = NULL;
231231
PyObject *layout_fields = NULL;
232232
PyObject *layout = NULL;
@@ -291,12 +291,12 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
291291
goto error;
292292
}
293293

294-
align = PyObject_GetAttr(layout, &_Py_ID(align));
295-
if (!align) {
294+
align_obj = PyObject_GetAttr(layout, &_Py_ID(align));
295+
if (!align_obj) {
296296
goto error;
297297
}
298-
Py_ssize_t total_align = PyLong_AsSsize_t(align);
299-
Py_CLEAR(align);
298+
Py_ssize_t total_align = PyLong_AsSsize_t(align_obj);
299+
Py_CLEAR(align_obj);
300300
if (total_align < 0) {
301301
if (!PyErr_Occurred()) {
302302
PyErr_SetString(PyExc_ValueError,
@@ -305,12 +305,12 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
305305
goto error;
306306
}
307307

308-
size = PyObject_GetAttr(layout, &_Py_ID(size));
309-
if (!size) {
308+
size_obj = PyObject_GetAttr(layout, &_Py_ID(size));
309+
if (!size_obj) {
310310
goto error;
311311
}
312-
Py_ssize_t total_size = PyLong_AsSsize_t(size);
313-
Py_CLEAR(size);
312+
Py_ssize_t total_size = PyLong_AsSsize_t(size_obj);
313+
Py_CLEAR(size_obj);
314314
if (total_size < 0) {
315315
if (!PyErr_Occurred()) {
316316
PyErr_SetString(PyExc_ValueError,
@@ -669,8 +669,8 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
669669
error:
670670
Py_XDECREF(layout_func);
671671
Py_XDECREF(kwnames);
672-
Py_XDECREF(align);
673-
Py_XDECREF(size);
672+
Py_XDECREF(align_obj);
673+
Py_XDECREF(size_obj);
674674
Py_XDECREF(layout_fields_obj);
675675
Py_XDECREF(layout_fields);
676676
Py_XDECREF(layout);

0 commit comments

Comments
 (0)
0