[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] python: Avoid memory leaks on libvirt_virNodeGetCPUStats



From: Alex Jia <ajia redhat com>

Detected by valgrind. Leaks are introduced in commit 4955602.

* python/libvirt-override.c (libvirt_virNodeGetCPUStats): fix memory leaks
and improve codes return value.

For details, please see the following link:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=770943

Signed-off-by: Alex Jia <ajia redhat com>
---
 python/libvirt-override.c |   46 ++++++++++++++++++++++++++++++--------------
 1 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 792cfa3..6e076bc 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -2503,7 +2503,9 @@ libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
 static PyObject *
 libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
 {
-    PyObject *ret;
+    PyObject *ret = NULL;
+    PyObject *key = NULL;
+    PyObject *val = NULL;
     PyObject *pyobj_conn;
     virConnectPtr conn;
     unsigned int flags;
@@ -2512,39 +2514,53 @@ libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
     virNodeCPUStatsPtr stats = NULL;
 
     if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCPUStats", &pyobj_conn, &cpuNum, &flags))
-        return(NULL);
+        return ret;
     conn = (virConnectPtr)(PyvirConnect_Get(pyobj_conn));
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     c_retval = virNodeGetCPUStats(conn, cpuNum, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
     if (c_retval < 0)
-        return VIR_PY_NONE;
+        return ret;
 
     if (nparams) {
         if (VIR_ALLOC_N(stats, nparams) < 0)
-            return VIR_PY_NONE;
+            return PyErr_NoMemory();
 
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virNodeGetCPUStats(conn, cpuNum, stats, &nparams, flags);
         LIBVIRT_END_ALLOW_THREADS;
-        if (c_retval < 0) {
-            VIR_FREE(stats);
-            return VIR_PY_NONE;
-        }
-    }
-    if (!(ret = PyDict_New())) {
-        VIR_FREE(stats);
-        return VIR_PY_NONE;
+        if (c_retval < 0)
+            goto error;
     }
+
+    if (!(ret = PyDict_New()))
+        goto error;
+
     for (i = 0; i < nparams; i++) {
-        PyDict_SetItem(ret,
-                       libvirt_constcharPtrWrap(stats[i].field),
-                       libvirt_ulonglongWrap(stats[i].value));
+        key = libvirt_constcharPtrWrap(stats[i].field);
+        val = libvirt_ulonglongWrap(stats[i].value);
+
+        if (!key || !val)
+            goto error;
+
+        if (PyDict_SetItem(ret, key, val) < 0) {
+            Py_DECREF(ret);
+            goto error;
+        }
+
+        Py_DECREF(key);
+        Py_DECREF(val);
     }
 
     VIR_FREE(stats);
     return ret;
+
+error:
+    VIR_FREE(stats);
+    Py_XDECREF(key);
+    Py_XDECREF(val);
+    return ret;
 }
 
 static PyObject *
-- 
1.7.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]