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

Stefan Berger stefanb at us.ibm.com
Mon Mar 10 19:10:09 UTC 2014


Eric Blake <eblake at redhat.com> wrote on 03/10/2014 02:09:58 PM:

> From: Eric Blake <eblake at redhat.com>
> To: Stefan Berger/Watson/IBM at IBMUS, libvir-list at redhat.com, 
> Cc: laine at laine.org
> Date: 03/10/2014 02:24 PM
> Subject: Re: [libvirt] [PATCH] BZ1072677: Avoid freeing of 0 file 
descriptor
> 
> 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'?

It's probably too general for the fd purpose here only. 

> 
> > +
> > +/* 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().

Also this would be possible.

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

So a simple memset() would do? Having a macro for VIR_ALLOC_N_FD() would 
probably be too much, eh?

Stefan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20140310/cb59a818/attachment-0001.htm>


More information about the libvir-list mailing list