mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-14 16:45:20 +00:00
python: Avoid memory leaks on libvirt_virNodeGetMemoryStats
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@redhat.com>
This commit is contained in:
parent
2791b8ab9b
commit
53b45aa494
@ -2550,7 +2550,9 @@ libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
|
libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *ret;
|
PyObject *ret = NULL;
|
||||||
|
PyObject *key = NULL;
|
||||||
|
PyObject *val = NULL;
|
||||||
PyObject *pyobj_conn;
|
PyObject *pyobj_conn;
|
||||||
virConnectPtr conn;
|
virConnectPtr conn;
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
@ -2559,7 +2561,7 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
|
|||||||
virNodeMemoryStatsPtr stats = NULL;
|
virNodeMemoryStatsPtr stats = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetMemoryStats", &pyobj_conn, &cellNum, &flags))
|
if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetMemoryStats", &pyobj_conn, &cellNum, &flags))
|
||||||
return(NULL);
|
return ret;
|
||||||
conn = (virConnectPtr)(PyvirConnect_Get(pyobj_conn));
|
conn = (virConnectPtr)(PyvirConnect_Get(pyobj_conn));
|
||||||
|
|
||||||
LIBVIRT_BEGIN_ALLOW_THREADS;
|
LIBVIRT_BEGIN_ALLOW_THREADS;
|
||||||
@ -2570,7 +2572,7 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
|
|||||||
|
|
||||||
if (nparams) {
|
if (nparams) {
|
||||||
if (VIR_ALLOC_N(stats, nparams) < 0)
|
if (VIR_ALLOC_N(stats, nparams) < 0)
|
||||||
return VIR_PY_NONE;
|
return PyErr_NoMemory();
|
||||||
|
|
||||||
LIBVIRT_BEGIN_ALLOW_THREADS;
|
LIBVIRT_BEGIN_ALLOW_THREADS;
|
||||||
c_retval = virNodeGetMemoryStats(conn, cellNum, stats, &nparams, flags);
|
c_retval = virNodeGetMemoryStats(conn, cellNum, stats, &nparams, flags);
|
||||||
@ -2580,18 +2582,32 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
|
|||||||
return VIR_PY_NONE;
|
return VIR_PY_NONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(ret = PyDict_New())) {
|
|
||||||
VIR_FREE(stats);
|
if (!(ret = PyDict_New()))
|
||||||
return VIR_PY_NONE;
|
goto error;
|
||||||
}
|
|
||||||
for (i = 0; i < nparams; i++) {
|
for (i = 0; i < nparams; i++) {
|
||||||
PyDict_SetItem(ret,
|
key = libvirt_constcharPtrWrap(stats[i].field);
|
||||||
libvirt_constcharPtrWrap(stats[i].field),
|
val = libvirt_ulonglongWrap(stats[i].value);
|
||||||
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);
|
VIR_FREE(stats);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
error:
|
||||||
|
VIR_FREE(stats);
|
||||||
|
Py_XDECREF(key);
|
||||||
|
Py_XDECREF(val);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
Loading…
x
Reference in New Issue
Block a user