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

Eric Blake eblake at redhat.com
Tue Mar 20 15:46:51 UTC 2012


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 at redhat.com>
> ---
>  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 at redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120320/533fbdaa/attachment-0001.sig>


More information about the libvir-list mailing list