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

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

 



On 03/20/2012 01:14 AM, Alex Jia wrote:
> 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, 31 insertions(+), 10 deletions(-)

>      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) {
> +            ret = NULL;
> +            goto error;

Memory leak of ret.

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

Fix it by doing:

if (!key || !val || PyDict_SetItem(ret, key, val) < 0) {
    Py_DECREF(ret);
    ret = NULL;
    goto error;
}

ACK with that fix.

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