diff options
author | Alex Jia <ajia@redhat.com> | 2012-03-22 09:54:03 +0100 |
---|---|---|
committer | Daniel Veillard <veillard@redhat.com> | 2012-03-22 20:15:00 +0800 |
commit | f14ce3e4bdae296653d3ebc03ba65f35d3120676 (patch) | |
tree | a8714e554e143f3e1e3afabf760f13822c655cfb /libvirt-override.c | |
parent | 7f39fff4c0ae96e8afa02ad50fa30d325a417c7a (diff) | |
download | libvirt-python-v6-f14ce3e4bdae296653d3ebc03ba65f35d3120676.tar.gz libvirt-python-v6-f14ce3e4bdae296653d3ebc03ba65f35d3120676.tar.xz libvirt-python-v6-f14ce3e4bdae296653d3ebc03ba65f35d3120676.zip |
python: Avoid memory leaks on libvirt_virNodeGetCPUStats
https://bugzilla.redhat.com/show_bug.cgi?id=770943
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>
(cherry picked from commit 558ebc256dc8179413b1f35d91cd5167e41633cb)
Diffstat (limited to 'libvirt-override.c')
-rw-r--r-- | libvirt-override.c | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/libvirt-override.c b/libvirt-override.c index 8ed9f73..22ee975 100644 --- a/libvirt-override.c +++ b/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,7 +2514,7 @@ 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; @@ -2523,7 +2525,7 @@ libvirt_virNodeGetCPUStats(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 = virNodeGetCPUStats(conn, cpuNum, stats, &nparams, flags); @@ -2533,18 +2535,32 @@ libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) return VIR_PY_NONE; } } - if (!(ret = PyDict_New())) { - VIR_FREE(stats); - return VIR_PY_NONE; - } + + 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 || PyDict_SetItem(ret, key, val) < 0) { + Py_DECREF(ret); + ret = NULL; + 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 * |