[libvirt] [PATCH] BZ1072677: Avoid freeing of 0 file descriptor

Eric Blake eblake at redhat.com
Mon Mar 10 18:09:58 UTC 2014


On 03/08/2014 04:29 PM, Stefan Berger wrote:
> From: Stefan Berger <stefanb at linux.vnet.ibm.com>
> 
> Avoid the freeing of an array of zero file descriptors in case
> of error. Introduce a macro VIR_INIT_N_FD to initialize such
> an array's elements to -1.
> 
> Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
> ---
>  src/qemu/qemu_hotplug.c | 14 +++++++++++---
>  src/util/virfile.h      | 12 ++++++++++++
>  2 files changed, 23 insertions(+), 3 deletions(-)
> 

> +++ b/src/util/virfile.h
> @@ -75,6 +75,18 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
>                   VIR_FILE_CLOSE_PRESERVE_ERRNO |    \
>                   VIR_FILE_CLOSE_DONT_LOG))
>  
> +static inline void vir_init_n_int(int *ptr, int count, int value)
> +{
> +    int i;
> +
> +    for (i = 0; i < count; i++)
> +        ptr[i] = value;
> +}

Do we ever plan on using this for values other than '-1'?

> +
> +/* Initialize an array of file descriptors to -1 */
> +# define VIR_INIT_N_FD(ptr, count) \
> +    vir_init_n_int(ptr, count, -1)

Could also be spelled:

memset(ptr, -1, sizeof(*ptr) * count))

which goes back to why we need vir_init_n_int().

I agree that the fix to qemu_hotplug.c to not close fd 0 on failure is
needed, but am not sure about the complexity of the virfile.h addition.


-- 
Eric Blake   eblake 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: 604 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20140310/6ddd1e91/attachment-0001.sig>


More information about the libvir-list mailing list