From e649c8032f84b488cac8ea6c8fb9a77c424a0419 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Graber?= Date: Thu, 11 Apr 2013 14:15:21 +0200 Subject: [PATCH] python: Fix memory management MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- src/python-lxc/lxc.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c index 06d339691..8da6f36dc 100644 --- a/src/python-lxc/lxc.c +++ b/src/python-lxc/lxc.c @@ -1,7 +1,7 @@ /* * python-lxc: Python bindings for LXC * - * (C) Copyright Canonical Ltd. 2012 + * (C) Copyright Canonical Ltd. 2012-2013 * * Authors: * Stéphane Graber @@ -43,9 +43,10 @@ convert_tuple_to_char_pointer_array(PyObject *argv) { PyObject *pyobj = PyTuple_GetItem(argv, i); char *str = NULL; - PyObject *pystr; + PyObject *pystr = NULL; if (!PyUnicode_Check(pyobj)) { PyErr_SetString(PyExc_ValueError, "Expected a string"); + free(result); return NULL; } @@ -62,6 +63,7 @@ convert_tuple_to_char_pointer_array(PyObject *argv) { static void Container_dealloc(Container* self) { + lxc_container_put(self->container); Py_TYPE(self)->tp_free((PyObject*)self); } @@ -190,9 +192,11 @@ Container_create(Container *self, PyObject *args, PyObject *kwds) } if (self->container->create(self->container, template_name, create_args)) { + free(create_args); Py_RETURN_TRUE; } + free(create_args); Py_RETURN_FALSE; } @@ -222,6 +226,7 @@ Container_get_cgroup_item(Container *self, PyObject *args, PyObject *kwds) static char *kwlist[] = {"key", NULL}; char* key = NULL; int len = 0; + PyObject *ret = NULL; if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist, &key)) @@ -235,10 +240,13 @@ Container_get_cgroup_item(Container *self, PyObject *args, PyObject *kwds) char* value = (char*) malloc(sizeof(char)*len + 1); if (self->container->get_cgroup_item(self->container, key, value, len + 1) != len) { + free(value); Py_RETURN_FALSE; } - return PyUnicode_FromString(value); + ret = PyUnicode_FromString(value); + free(value); + return ret; } static PyObject * @@ -247,6 +255,7 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds) static char *kwlist[] = {"key", NULL}; char* key = NULL; int len = 0; + PyObject *ret = NULL; if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist, &key)) @@ -260,10 +269,13 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds) char* value = (char*) malloc(sizeof(char)*len + 1); if (self->container->get_config_item(self->container, key, value, len + 1) != len) { + free(value); Py_RETURN_FALSE; } - return PyUnicode_FromString(value); + ret = PyUnicode_FromString(value); + free(value); + return ret; } static PyObject * @@ -278,6 +290,7 @@ Container_get_keys(Container *self, PyObject *args, PyObject *kwds) static char *kwlist[] = {"key", NULL}; char* key = NULL; int len = 0; + PyObject *ret = NULL; if (! PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist, &key)) @@ -291,10 +304,13 @@ Container_get_keys(Container *self, PyObject *args, PyObject *kwds) char* value = (char*) malloc(sizeof(char)*len + 1); if (self->container->get_keys(self->container, key, value, len + 1) != len) { + free(value); Py_RETURN_FALSE; } - return PyUnicode_FromString(value); + ret = PyUnicode_FromString(value); + free(value); + return ret; } static PyObject * @@ -427,9 +443,11 @@ Container_start(Container *self, PyObject *args, PyObject *kwds) self->container->want_daemonize(self->container); if (self->container->start(self->container, init_useinit, init_args)) { + free(init_args); Py_RETURN_TRUE; } + free(init_args); Py_RETURN_FALSE; } -- 2.47.2