From: Noel Power Date: Wed, 23 Jan 2019 15:15:07 +0000 (+0000) Subject: Cleanup references to module objects returned from PyImport_ImportModule X-Git-Tag: ldb-1.6.1~223 X-Git-Url: http://git.samba.org/samba.git/?p=gd%2Fsamba-autobuild%2F.git;a=commitdiff_plain;h=53d973f59c3514f9c288cc8e53a682176a5fc415 Cleanup references to module objects returned from PyImport_ImportModule Signed-off-by: Noel Power Reviewed-by: Douglas Bagnall --- diff --git a/lib/ldb-samba/pyldb.c b/lib/ldb-samba/pyldb.c index 57c5397bc06..0cf550bc7a5 100644 --- a/lib/ldb-samba/pyldb.c +++ b/lib/ldb-samba/pyldb.c @@ -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"); - if (PyAuthSession_Type == NULL) + if (PyAuthSession_Type == NULL) { + Py_CLEAR(mod_samba_auth); return NULL; + } 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"); - if (PySambaLdb.tp_base == NULL) + if (PySambaLdb.tp_base == NULL) { + Py_CLEAR(pyldb_module); return NULL; + } py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError"); + Py_CLEAR(pyldb_module); + if (PyType_Ready(&PySambaLdb) < 0) return NULL; diff --git a/source4/dsdb/pydsdb.c b/source4/dsdb/pydsdb.c index 3a1e97cf47f..e3c1fd59889 100644 --- a/source4/dsdb/pydsdb.c +++ b/source4/dsdb/pydsdb.c @@ -53,10 +53,13 @@ static PyObject *py_ldb_get_exception(void) { PyObject *mod = PyImport_ImportModule("ldb"); + PyObject *result = 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) @@ -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_DECREF(module); return NULL; } + + Py_CLEAR(module); + py_ret = py_type->tp_alloc(py_type, 0); + Py_CLEAR(py_type); if (py_ret == NULL) { - Py_DECREF(py_type); 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) { - Py_DECREF(py_type); - Py_DECREF(py_ret); + Py_CLEAR(py_ret); PyErr_NoMemory(); return NULL; } diff --git a/source4/libnet/py_net.c b/source4/libnet/py_net.c index 05afe3b9c48..3d95854e732 100644 --- a/source4/libnet/py_net.c +++ b/source4/libnet/py_net.c @@ -40,8 +40,12 @@ 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() */ @@ -98,10 +102,12 @@ static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err, const break; } } - PyErr_SetObject(error, + if (error) { + PyErr_SetObject(error, 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) diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index bcbd94714d0..e00318a96be 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -555,16 +555,22 @@ MODULE_INIT_FUNC(base) return NULL; BaseObject_Type = (PyTypeObject *)PyObject_GetAttrString(dep_talloc, "BaseObject"); - if (BaseObject_Type == NULL) + if (BaseObject_Type == NULL) { + Py_CLEAR(dep_talloc); return NULL; + } + Py_CLEAR(dep_talloc); dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc"); - if (dep_samba_dcerpc_misc == NULL) + if (dep_samba_dcerpc_misc == NULL) { return NULL; + } 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; + } 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(); - if (PyType_Ready(&dcerpc_InterfaceType) < 0) + if (PyType_Ready(&dcerpc_InterfaceType) < 0) { return NULL; + } - if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0) + if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0) { return NULL; - if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0) + } + if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0) { return NULL; - if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0) + } + if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0) { return NULL; + } - if (PyType_Ready(&py_dcerpc_ndr_pointer_type) < 0) + if (PyType_Ready(&py_dcerpc_ndr_pointer_type) < 0) { return NULL; + } m = PyModule_Create(&moduledef); - if (m == NULL) + if (m == NULL) { return NULL; + } Py_INCREF((PyObject *)&dcerpc_InterfaceType); PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType); diff --git a/source4/librpc/rpc/pyrpc_util.c b/source4/librpc/rpc/pyrpc_util.c index 6dadc214ea5..29fd281f54d 100644 --- a/source4/librpc/rpc/pyrpc_util.c +++ b/source4/librpc/rpc/pyrpc_util.c @@ -392,6 +392,7 @@ PyObject *py_return_ndr_struct(const char *module_name, const char *type_name, { PyTypeObject *py_type; PyObject *module; + PyObject *result = NULL; 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 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) diff --git a/source4/param/provision.c b/source4/param/provision.c index 48a471cf44d..10a657b981b 100644 --- a/source4/param/provision.c +++ b/source4/param/provision.c @@ -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) { - 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; } + 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); - 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,