]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35529: Fix a reference counting bug in PyCFuncPtr_FromDll(). (GH-11229)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 20 Dec 2018 08:47:59 +0000 (00:47 -0800)
committerGitHub <noreply@github.com>
Thu, 20 Dec 2018 08:47:59 +0000 (00:47 -0800)
"dll" would leak if an error occurred in _validate_paramflags() or
GenericPyCData_new().
(cherry picked from commit d77d97c9a1f593fe161afab97e2a3e2292ab88b9)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Modules/_ctypes/_ctypes.c

index 937375a4ea44275b95b239504b1d1f58ad6cce8b..f98eabbb370b50a1d3373c35e3e6253f82c62dc8 100644 (file)
@@ -3409,20 +3409,23 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 #endif
-    Py_INCREF(dll); /* for KeepRef */
-    Py_DECREF(ftuple);
-    if (!_validate_paramflags(type, paramflags))
+    if (!_validate_paramflags(type, paramflags)) {
+        Py_DECREF(ftuple);
         return NULL;
+    }
 
     self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
-    if (!self)
+    if (!self) {
+        Py_DECREF(ftuple);
         return NULL;
+    }
 
     Py_XINCREF(paramflags);
     self->paramflags = paramflags;
 
     *(void **)self->b_ptr = address;
-
+    Py_INCREF(dll);
+    Py_DECREF(ftuple);
     if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
         Py_DECREF((PyObject *)self);
         return NULL;