[libvirt] [PATCH] python: Expose blockPeek API to python binding

Eric Blake eblake at redhat.com
Tue Dec 13 18:35:33 UTC 2011


On 12/13/2011 06:32 AM, Osier Yang wrote:
> ---
>  python/generator.py             |    1 +
>  python/libvirt-override-api.xml |    9 +++++++++
>  python/libvirt-override.c       |   35 +++++++++++++++++++++++++++++++++++
>  3 files changed, 45 insertions(+), 0 deletions(-)
> 

> +
> +    if ((buf = malloc(size)) == NULL)
> +        return VIR_PY_NONE;
> +
> +    LIBVIRT_BEGIN_ALLOW_THREADS;
> +    c_retval = virDomainBlockPeek(domain, disk, offset, size, buf, flags);
> +    LIBVIRT_END_ALLOW_THREADS;
> +
> +    if (c_retval < 0)
> +        return VIR_PY_NONE;

Memory leak.  Free buf on this error path.

> +
> +    py_retval = libvirt_charPtrWrap(buf);
> +    return py_retval;
> +}

Wrong.  buf may contain embedded NUL, and/or not be NUL-terminated, but
PyString_FromString copies its argument up to the NUL byte, meaning you
leaked buf, and either truncated or had a buffer overrun in the
resulting python object.

You need to return a python object that represents an array of bytes (of
length determined by the input argument), and NOT a generic string.

-- 
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/20111213/b319da78/attachment-0001.sig>


More information about the libvir-list mailing list