Cleanup references to module objects returned from PyImport_ImportModule
authorNoel Power <noel.power@suse.com>
Wed, 23 Jan 2019 15:15:07 +0000 (15:15 +0000)
committerNoel Power <npower@samba.org>
Thu, 7 Feb 2019 12:44:30 +0000 (13:44 +0100)
Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
lib/ldb-samba/pyldb.c
source4/dsdb/pydsdb.c
source4/libnet/py_net.c
source4/librpc/rpc/pyrpc.c
source4/librpc/rpc/pyrpc_util.c
source4/param/provision.c

index 57c5397bc0689ebc09edd8a2370f974666c91508..0cf550bc7a5b2a895eb92df1df903c39825a0e70 100644 (file)
@@ -180,8 +180,10 @@ static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
                return NULL;
 
        PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "session_info");
                return NULL;
 
        PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "session_info");
-       if (PyAuthSession_Type == NULL)
+       if (PyAuthSession_Type == NULL) {
+               Py_CLEAR(mod_samba_auth);
                return NULL;
                return NULL;
+       }
 
        ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
 
 
        ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
 
@@ -262,11 +264,15 @@ MODULE_INIT_FUNC(_ldb)
                return NULL;
 
        PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
                return NULL;
 
        PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
-       if (PySambaLdb.tp_base == NULL)
+       if (PySambaLdb.tp_base == NULL) {
+               Py_CLEAR(pyldb_module);
                return NULL;
                return NULL;
+       }
 
        py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
 
 
        py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
 
+       Py_CLEAR(pyldb_module);
+
        if (PyType_Ready(&PySambaLdb) < 0)
                return NULL;
 
        if (PyType_Ready(&PySambaLdb) < 0)
                return NULL;
 
index 3a1e97cf47f32f11b203429f3387de4f928f6c4b..e3c1fd598890d3cab9c3b347d9d41d53e1d3cf16 100644 (file)
 static PyObject *py_ldb_get_exception(void)
 {
        PyObject *mod = PyImport_ImportModule("ldb");
 static PyObject *py_ldb_get_exception(void)
 {
        PyObject *mod = PyImport_ImportModule("ldb");
+       PyObject *result = NULL;
        if (mod == NULL)
                return NULL;
 
        if (mod == NULL)
                return NULL;
 
-       return PyObject_GetAttrString(mod, "LdbError");
+       result = PyObject_GetAttrString(mod, "LdbError");
+       Py_CLEAR(mod);
+       return result;
 }
 
 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
 }
 
 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
@@ -741,11 +744,15 @@ static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
 
        py_type = (PyTypeObject *)PyObject_GetAttrString(module, "MessageElement");
        if (py_type == NULL) {
 
        py_type = (PyTypeObject *)PyObject_GetAttrString(module, "MessageElement");
        if (py_type == NULL) {
+               Py_DECREF(module);
                return NULL;
        }
                return NULL;
        }
+
+       Py_CLEAR(module);
+
        py_ret = py_type->tp_alloc(py_type, 0);
        py_ret = py_type->tp_alloc(py_type, 0);
+       Py_CLEAR(py_type);
        if (py_ret == NULL) {
        if (py_ret == NULL) {
-               Py_DECREF(py_type);
                PyErr_NoMemory();
                return NULL;
        }
                PyErr_NoMemory();
                return NULL;
        }
@@ -753,8 +760,7 @@ static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
 
        ret->mem_ctx = talloc_new(NULL);
        if (talloc_reference(ret->mem_ctx, new_el) == NULL) {
 
        ret->mem_ctx = talloc_new(NULL);
        if (talloc_reference(ret->mem_ctx, new_el) == NULL) {
-               Py_DECREF(py_type);
-               Py_DECREF(py_ret);
+               Py_CLEAR(py_ret);
                PyErr_NoMemory();
                return NULL;
        }
                PyErr_NoMemory();
                return NULL;
        }
index 05afe3b9c48e0c74f324e0a0f16a635354cc832b..3d95854e73202e5bae867ea80db61fd6b0c82b67 100644 (file)
 
 static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err, const char *error_description)
 {
 
 static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err, const char *error_description)
 {
-       PyObject *error = PyObject_GetAttrString(PyImport_ImportModule("samba"),
-                                                "DsExtendedError");
+       PyObject *mod = NULL;
+       PyObject *error = NULL;
+       mod = PyImport_ImportModule("samba");
+       if (mod) {
+               error = PyObject_GetAttrString(mod, "DsExtendedError");
+       }
        if (error_description == NULL) {
                switch (ext_err) {
                        /* Copied out of ndr_drsuapi.c:ndr_print_drsuapi_DsExtendedError() */
        if (error_description == NULL) {
                switch (ext_err) {
                        /* Copied out of ndr_drsuapi.c:ndr_print_drsuapi_DsExtendedError() */
@@ -98,10 +102,12 @@ static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err, const
                                break;
                }
        }
                                break;
                }
        }
-       PyErr_SetObject(error,
+       if (error) {
+               PyErr_SetObject(error,
                        Py_BuildValue(discard_const_p(char, "(i,s)"),
                                      ext_err,
                                      error_description));
                        Py_BuildValue(discard_const_p(char, "(i,s)"),
                                      ext_err,
                                      error_description));
+       }
 }
 
 static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
 }
 
 static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
index bcbd94714d06f46c66df41477c0e3f4f1562774b..e00318a96be21f3bfe4840d4fe578e8aeb40c5ad 100644 (file)
@@ -555,16 +555,22 @@ MODULE_INIT_FUNC(base)
                return NULL;
 
        BaseObject_Type = (PyTypeObject *)PyObject_GetAttrString(dep_talloc, "BaseObject");
                return NULL;
 
        BaseObject_Type = (PyTypeObject *)PyObject_GetAttrString(dep_talloc, "BaseObject");
-       if (BaseObject_Type == NULL)
+       if (BaseObject_Type == NULL) {
+               Py_CLEAR(dep_talloc);
                return NULL;
                return NULL;
+       }
 
 
+       Py_CLEAR(dep_talloc);
        dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc");
        dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc");
-       if (dep_samba_dcerpc_misc == NULL)
+       if (dep_samba_dcerpc_misc == NULL) {
                return NULL;
                return NULL;
+       }
 
        ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id");
 
        ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id");
-       if (ndr_syntax_id_Type == NULL)
+       Py_CLEAR(dep_samba_dcerpc_misc);
+       if (ndr_syntax_id_Type == NULL) {
                return NULL;
                return NULL;
+       }
 
        py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type;
        py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
 
        py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type;
        py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
@@ -576,22 +582,28 @@ MODULE_INIT_FUNC(base)
        py_dcerpc_ndr_pointer_type.tp_base = BaseObject_Type;
        py_dcerpc_ndr_pointer_type.tp_basicsize = pytalloc_BaseObject_size();
 
        py_dcerpc_ndr_pointer_type.tp_base = BaseObject_Type;
        py_dcerpc_ndr_pointer_type.tp_basicsize = pytalloc_BaseObject_size();
 
-       if (PyType_Ready(&dcerpc_InterfaceType) < 0)
+       if (PyType_Ready(&dcerpc_InterfaceType) < 0) {
                return NULL;
                return NULL;
+       }
 
 
-       if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0)
+       if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0) {
                return NULL;
                return NULL;
-       if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0)
+       }
+       if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0) {
                return NULL;
                return NULL;
-       if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0)
+       }
+       if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0) {
                return NULL;
                return NULL;
+       }
 
 
-       if (PyType_Ready(&py_dcerpc_ndr_pointer_type) < 0)
+       if (PyType_Ready(&py_dcerpc_ndr_pointer_type) < 0) {
                return NULL;
                return NULL;
+       }
 
        m = PyModule_Create(&moduledef);
 
        m = PyModule_Create(&moduledef);
-       if (m == NULL)
+       if (m == NULL) {
                return NULL;
                return NULL;
+       }
 
        Py_INCREF((PyObject *)&dcerpc_InterfaceType);
        PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
 
        Py_INCREF((PyObject *)&dcerpc_InterfaceType);
        PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
index 6dadc214ea52de1460c18008a7a9901f2a879856..29fd281f54daddac19925730c4777318a3a07bc2 100644 (file)
@@ -392,6 +392,7 @@ PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
 {
        PyTypeObject *py_type;
        PyObject *module;
 {
        PyTypeObject *py_type;
        PyObject *module;
+       PyObject *result = NULL;
 
        if (r == NULL) {
                Py_RETURN_NONE;
 
        if (r == NULL) {
                Py_RETURN_NONE;
@@ -408,7 +409,10 @@ PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
                return NULL;
        }
 
                return NULL;
        }
 
-       return pytalloc_reference_ex(py_type, r_ctx, r);
+       result = pytalloc_reference_ex(py_type, r_ctx, r);
+       Py_CLEAR(module);
+       Py_CLEAR(py_type);
+       return result;
 }
 
 PyObject *PyString_FromStringOrNULL(const char *str)
 }
 
 PyObject *PyString_FromStringOrNULL(const char *str)
index 48a471cf44d70f7ed8a62b67effcf59de8dacaae..10a657b981be1f8618146056d4cbb7ae2f0c47b3 100644 (file)
@@ -201,19 +201,23 @@ NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
 
 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
 {
 
 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
 {
-       PyObject *mod_security, *dom_sid_Type;
+       PyObject *mod_security = NULL, *dom_sid_Type = NULL, *result = NULL;
 
        mod_security = PyImport_ImportModule("samba.dcerpc.security");
        if (mod_security == NULL) {
                return NULL;
        }
 
        mod_security = PyImport_ImportModule("samba.dcerpc.security");
        if (mod_security == NULL) {
                return NULL;
        }
+
        dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
        if (dom_sid_Type == NULL) {
                Py_DECREF(mod_security);
                return NULL;
        }
        dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
        if (dom_sid_Type == NULL) {
                Py_DECREF(mod_security);
                return NULL;
        }
+
+       result = pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
        Py_DECREF(mod_security);
        Py_DECREF(mod_security);
-       return pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
+       Py_DECREF(dom_sid_Type);
+       return result;
 }
 
 NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
 }
 
 NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,