From abc7f912f66d41dd734a10900429d4cad9377da5 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Mon, 11 Apr 2011 14:30:36 +0200 Subject: Fixed several memory leaks Several places python-ethtool leaked memory, mostly due to missing Py_DECREF() calls on objects being put in to python lists (via PyList_Append() calls). This revealed an issue in addition where the IPv6 addresses pointers in some cases could freed more times. This is fixed as well. Signed-off-by: David Sommerseth --- python-ethtool/ethtool.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'python-ethtool/ethtool.c') diff --git a/python-ethtool/ethtool.c b/python-ethtool/ethtool.c index a29f052..60e407c 100644 --- a/python-ethtool/ethtool.c +++ b/python-ethtool/ethtool.c @@ -93,8 +93,11 @@ static PyObject *get_active_devices(PyObject *self __unused, PyObject *args __un ifr = ifc.ifc_req; for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { if (!(ioctl(skfd, SIOCGIFFLAGS, ifr) < 0)) - if (ifr->ifr_flags & IFF_UP) - PyList_Append(list, PyString_FromString(ifr->ifr_name)); + if (ifr->ifr_flags & IFF_UP) { + PyObject *str = PyString_FromString(ifr->ifr_name); + PyList_Append(list, str); + Py_DECREF(str); + } ifr++; } @@ -118,6 +121,7 @@ static PyObject *get_devices(PyObject *self __unused, PyObject *args __unused) /* skip over first two lines */ ret = fgets(buffer, 256, fd); ret = fgets(buffer, 256, fd); while (!feof(fd)) { + PyObject *str; char *name = buffer; char *end = buffer; @@ -129,7 +133,10 @@ static PyObject *get_devices(PyObject *self __unused, PyObject *args __unused) *end = 0; /* terminate where colon was */ while (*name == ' ') name++; /* skip over leading whitespace if any */ - PyList_Append(list, PyString_FromString(name)); + + str = PyString_FromString(name); + PyList_Append(list, str); + Py_DECREF(str); } fclose(fd); return list; @@ -320,7 +327,9 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) { PyObject *dev = PyObject_CallObject((PyObject *)ðtool_etherinfoType, args); if( dev ) { PyList_Append(devlist, dev); + Py_DECREF(dev); } + Py_DECREF(args); } } if( fetch_devs_len > 0 ) { -- cgit