From b615258d6df8ad867cee99bdccffff05127fbc92 Mon Sep 17 00:00:00 2001 From: Pavel Reichl Date: Mon, 29 Sep 2014 16:40:53 +0100 Subject: [PATCH 22/22] pyhbac,pysss: fix reference leaks Resolves: https://fedorahosted.org/sssd/ticket/1195 Reviewed-by: Jakub Hrozek --- src/python/pyhbac.c | 22 +++++++++++++--------- src/python/pysss.c | 10 +++++++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/python/pyhbac.c b/src/python/pyhbac.c index e9dce9b01b78401516b5c21141be8007b4a94aec..dd345a6eb4db8ac6104251d5b9c8f11a160e280d 100644 --- a/src/python/pyhbac.c +++ b/src/python/pyhbac.c @@ -139,14 +139,17 @@ sequence_as_string_list(PyObject *seq, const char *paramname) utf_item = get_utf8_string(item, p); if (utf_item == NULL) { + Py_DECREF(item); return NULL; } ret[i] = py_strdup(PyString_AsString(utf_item)); Py_DECREF(utf_item); if (!ret[i]) { + Py_DECREF(item); return NULL; } + Py_DECREF(item); } ret[i] = NULL; @@ -242,10 +245,7 @@ str_concat_sequence(PyObject *seq, const char *delim) if (item == NULL) goto fail; part = PyString_AsString(item); - if (part == NULL) { - Py_DECREF(item); - goto fail; - } + if (part == NULL) goto fail; if (s) { s = py_strcat_realloc(s, delim); @@ -260,7 +260,9 @@ str_concat_sequence(PyObject *seq, const char *delim) } return s; + fail: + Py_XDECREF(item); PyMem_Free(s); return NULL; } @@ -269,11 +271,13 @@ fail: static void set_hbac_exception(PyObject *exc, struct hbac_info *error) { - PyErr_SetObject(exc, - Py_BuildValue(sss_py_const_p(char, "(i,s)"), - error->code, - error->rule_name ? \ - error->rule_name : "no rule")); + PyObject *obj; + + obj = Py_BuildValue(sss_py_const_p(char, "(i,s)"), error->code, + error->rule_name ? error->rule_name : "no rule"); + + PyErr_SetObject(exc, obj); + Py_XDECREF(obj); } /* ==================== HBAC Rule Element ========================*/ diff --git a/src/python/pysss.c b/src/python/pysss.c index 8f98f081c9600b74f5953faef40cb40b30388319..9e899f1399553a950e41e157612a7360de4c07b5 100644 --- a/src/python/pysss.c +++ b/src/python/pysss.c @@ -850,7 +850,7 @@ static PyObject *PySssLocalObject_new(PyTypeObject *type, if (confdb_path == NULL) { talloc_free(mem_ctx); PyErr_NoMemory(); - return NULL; + goto fail; } /* Connect to the conf db */ @@ -859,7 +859,7 @@ static PyObject *PySssLocalObject_new(PyTypeObject *type, talloc_free(mem_ctx); PyErr_SetSssErrorWithMessage(ret, "Could not initialize connection to the confdb\n"); - return NULL; + goto fail; } ret = sssd_domain_init(self->mem_ctx, self->confdb, "local", @@ -868,7 +868,7 @@ static PyObject *PySssLocalObject_new(PyTypeObject *type, talloc_free(mem_ctx); PyErr_SetSssErrorWithMessage(ret, "Could not initialize connection to the sysdb\n"); - return NULL; + goto fail; } self->sysdb = self->local->sysdb; @@ -876,6 +876,10 @@ static PyObject *PySssLocalObject_new(PyTypeObject *type, self->unlock = DO_UNLOCK; return (PyObject *) self; + +fail: + Py_DECREF(self); + return NULL; } /* -- 1.9.3