Re: [PATCHv2] python: Avoid memory leaks on libvirt_virNodeGetMemoryStats

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

 



On 02/14/2012 04:19 AM, ajia@xxxxxxxxxx wrote:
> 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 |   43 +++++++++++++++++++++++++++++--------------
>  1 files changed, 29 insertions(+), 14 deletions(-)
> 
> diff --git a/python/libvirt-override.c b/python/libvirt-override.c
> index 4e8a97e..594aae6 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 *ret = VIR_PY_NONE;

NAK - this increments the reference count on the None object...

> +    PyObject *key = NULL;
> +    PyObject *val = NULL;
>      PyObject *pyobj_conn;
>      virConnectPtr conn;
>      unsigned int flags;
> @@ -2442,32 +2444,45 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
>      c_retval = virNodeGetMemoryStats(conn, cellNum, 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();

...but this fails to decrement the reference count, so you've leaked a
resource.  See my comments on v1.

> +
> +        if (PyDict_SetItem(ret, key, val) < 0) {
> +            Py_DECREF(ret);
> +            goto error;

And here, you have a python exception from PyDict_SetItem...

> +        }
> +
> +        Py_DECREF(key);
> +        Py_DECREF(val);
>      }
>  
>      VIR_FREE(stats);
>      return ret;
> +
> +error:
> +    VIR_FREE(stats);
> +    Py_XDECREF(key);
> +    Py_XDECREF(val);
> +    return ret;

...so here, you _want_ to return NULL, not VIR_PY_NONE.

-- 
Eric Blake   eblake@xxxxxxxxxx    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

--
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]