[PATCH] python: Avoid memory leaks on libvirt_virNodeGetMemoryStats

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

 



From: Alex Jia <ajia@xxxxxxxxxx>

Detected by valgrind. Leaks are introduced in commit 17c7795.

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

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

Signed-off-by: Alex Jia <ajia@xxxxxxxxxx>
---
 python/libvirt-override.c |   41 ++++++++++++++++++++++++++++++-----------
 1 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 4e8a97e..ecb11ea 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -2426,7 +2426,9 @@ libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
 static PyObject *
 libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
 {
-    PyObject *ret;
+    PyObject *info, *ret;
+    PyObject *key = NULL;
+    PyObject *val = NULL;
     PyObject *pyobj_conn;
     virConnectPtr conn;
     unsigned int flags;
@@ -2446,28 +2448,45 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
 
     if (nparams) {
         if (VIR_ALLOC_N(stats, nparams) < 0)
-            return VIR_PY_NONE;
+            return PyErr_NoMemory();
 
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virNodeGetMemoryStats(conn, cellNum, stats, &nparams, flags);
         LIBVIRT_END_ALLOW_THREADS;
         if (c_retval < 0) {
-            VIR_FREE(stats);
-            return VIR_PY_NONE;
+            ret = VIR_PY_NONE;
+            goto cleanup;
         }
     }
-    if (!(ret = PyDict_New())) {
-        VIR_FREE(stats);
-        return VIR_PY_NONE;
+    if (!(info = PyDict_New())) {
+        ret = VIR_PY_NONE;
+        goto cleanup;
     }
+
     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 cleanup;
+
+        if (PyDict_SetItem(info, key, val) < 0) {
+            Py_DECREF(info);
+            goto cleanup;
+        }
+
+        Py_DECREF(key);
+        Py_DECREF(val);
     }
 
     VIR_FREE(stats);
-    return ret;
+    return info;
+
+cleanup:
+    VIR_FREE(stats);
+    Py_XDECREF(key);
+    Py_XDECREF(val);
+    return NULL;
 }
 
 static PyObject *
-- 
1.7.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]