Skip to content

Commit 4b6caac

Browse files
bpo-35529: Fix a reference counting bug in PyCFuncPtr_FromDll(). (GH-11229)
"dll" would leak if an error occurred in _validate_paramflags() or GenericPyCData_new(). (cherry picked from commit d77d97c) Co-authored-by: Zackery Spytz <[email protected]>
1 parent a26201c commit 4b6caac

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,20 +3409,23 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
34093409
return NULL;
34103410
}
34113411
#endif
3412-
Py_INCREF(dll); /* for KeepRef */
3413-
Py_DECREF(ftuple);
3414-
if (!_validate_paramflags(type, paramflags))
3412+
if (!_validate_paramflags(type, paramflags)) {
3413+
Py_DECREF(ftuple);
34153414
return NULL;
3415+
}
34163416

34173417
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3418-
if (!self)
3418+
if (!self) {
3419+
Py_DECREF(ftuple);
34193420
return NULL;
3421+
}
34203422

34213423
Py_XINCREF(paramflags);
34223424
self->paramflags = paramflags;
34233425

34243426
*(void **)self->b_ptr = address;
3425-
3427+
Py_INCREF(dll);
3428+
Py_DECREF(ftuple);
34263429
if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
34273430
Py_DECREF((PyObject *)self);
34283431
return NULL;

0 commit comments

Comments
 (0)