]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
fdstream: avoid double close bug
authorEric Blake <eblake@redhat.com>
Wed, 30 May 2012 15:20:37 +0000 (09:20 -0600)
committerCole Robinson <crobinso@redhat.com>
Fri, 15 Jun 2012 14:58:27 +0000 (10:58 -0400)
Wen Congyang reported that we have a double-close bug if we fail
virFDStreamOpenInternal, since childfd duplicated one of the fds[]
array contents.  In truth, since we always transfer both members
of fds to other variables, we should close the fds through those
other names, and just use fds[] for pipe().

Bug present since 0.9.0 (commit e886237a).

* src/fdstream.c (virFDStreamOpenFileInternal): Swap scope of
childfd and fds[], to avoid a double close.
(cherry picked from commit f3cfc7c8848006b613e09bb1acd081716854527e)

src/fdstream.c

index 91942a874bb61a576cc18d0aefcb9cd6c5b8981c..738c2b891897f979aa3ca15affc27e580293511c 100644 (file)
@@ -573,7 +573,7 @@ virFDStreamOpenFileInternal(virStreamPtr st,
                             int mode)
 {
     int fd = -1;
-    int fds[2] = { -1, -1 };
+    int childfd = -1;
     struct stat sb;
     virCommandPtr cmd = NULL;
     int errfd = -1;
@@ -615,7 +615,7 @@ virFDStreamOpenFileInternal(virStreamPtr st,
     if ((st->flags & VIR_STREAM_NONBLOCK) &&
         (!S_ISCHR(sb.st_mode) &&
          !S_ISFIFO(sb.st_mode))) {
-        int childfd;
+        int fds[2] = { -1, -1 };
 
         if ((oflags & O_ACCMODE) == O_RDWR) {
             streamsReportError(VIR_ERR_INTERNAL_ERROR,
@@ -661,9 +661,8 @@ virFDStreamOpenFileInternal(virStreamPtr st,
 
 error:
     virCommandFree(cmd);
-    VIR_FORCE_CLOSE(fds[0]);
-    VIR_FORCE_CLOSE(fds[1]);
     VIR_FORCE_CLOSE(fd);
+    VIR_FORCE_CLOSE(childfd);
     VIR_FORCE_CLOSE(errfd);
     if (oflags & O_CREAT)
         unlink(path);