Skip to content

Commit 3752bc9

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 89b5ea2 commit 3752bc9

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
@@ -3526,20 +3526,23 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
35263526
return NULL;
35273527
}
35283528
#endif
3529-
Py_INCREF(dll); /* for KeepRef */
3530-
Py_DECREF(ftuple);
3531-
if (!_validate_paramflags(type, paramflags))
3529+
if (!_validate_paramflags(type, paramflags)) {
3530+
Py_DECREF(ftuple);
35323531
return NULL;
3532+
}
35333533

35343534
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3535-
if (!self)
3535+
if (!self) {
3536+
Py_DECREF(ftuple);
35363537
return NULL;
3538+
}
35373539

35383540
Py_XINCREF(paramflags);
35393541
self->paramflags = paramflags;
35403542

35413543
*(void **)self->b_ptr = address;
3542-
3544+
Py_INCREF(dll);
3545+
Py_DECREF(ftuple);
35433546
if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
35443547
Py_DECREF((PyObject *)self);
35453548
return NULL;

0 commit comments

Comments
 (0)