<tt><font size=2>Eric Blake <eblake@redhat.com> wrote on 03/10/2014
02:09:58 PM:<br>
<br>
> From: Eric Blake <eblake@redhat.com></font></tt>
<br><tt><font size=2>> To: Stefan Berger/Watson/IBM@IBMUS, libvir-list@redhat.com,
</font></tt>
<br><tt><font size=2>> Cc: laine@laine.org</font></tt>
<br><tt><font size=2>> Date: 03/10/2014 02:24 PM</font></tt>
<br><tt><font size=2>> Subject: Re: [libvirt] [PATCH] BZ1072677: Avoid
freeing of 0 file descriptor</font></tt>
<br><tt><font size=2>> <br>
> On 03/08/2014 04:29 PM, Stefan Berger wrote:<br>
> > From: Stefan Berger <stefanb@linux.vnet.ibm.com><br>
> > <br>
> > Avoid the freeing of an array of zero file descriptors in case<br>
> > of error. Introduce a macro VIR_INIT_N_FD to initialize such<br>
> > an array's elements to -1.<br>
> > <br>
> > Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com><br>
> > ---<br>
> >  src/qemu/qemu_hotplug.c | 14 +++++++++++---<br>
> >  src/util/virfile.h      | 12 ++++++++++++<br>
> >  2 files changed, 23 insertions(+), 3 deletions(-)<br>
> > <br>
> <br>
> > +++ b/src/util/virfile.h<br>
> > @@ -75,6 +75,18 @@ FILE *virFileFdopen(int *fdptr, const char
<br>
> *mode) ATTRIBUTE_RETURN_CHECK;<br>
> >                  
VIR_FILE_CLOSE_PRESERVE_ERRNO |    \<br>
> >                  
VIR_FILE_CLOSE_DONT_LOG))<br>
> >  <br>
> > +static inline void vir_init_n_int(int *ptr, int count, int value)<br>
> > +{<br>
> > +    int i;<br>
> > +<br>
> > +    for (i = 0; i < count; i++)<br>
> > +        ptr[i] = value;<br>
> > +}<br>
> <br>
> Do we ever plan on using this for values other than '-1'?</font></tt>
<br>
<br><tt><font size=2>It's probably too general for the fd purpose here
only. </font></tt>
<br><tt><font size=2><br>
> <br>
> > +<br>
> > +/* Initialize an array of file descriptors to -1 */<br>
> > +# define VIR_INIT_N_FD(ptr, count) \<br>
> > +    vir_init_n_int(ptr, count, -1)<br>
> <br>
> Could also be spelled:<br>
> <br>
> memset(ptr, -1, sizeof(*ptr) * count))<br>
> <br>
> which goes back to why we need vir_init_n_int().</font></tt>
<br>
<br><tt><font size=2>Also this would be possible.</font></tt>
<br><tt><font size=2><br>
> <br>
> I agree that the fix to qemu_hotplug.c to not close fd 0 on failure
is<br>
> needed, but am not sure about the complexity of the virfile.h addition.<br>
></font></tt>
<br>
<br><tt><font size=2>So a simple memset() would do? Having a macro for
VIR_ALLOC_N_FD() would probably be too much, eh?</font></tt>
<br>
<br><tt><font size=2>Stefan</font></tt>
<br>