]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: avoid null pointer dereference
authorEric Blake <eblake@redhat.com>
Tue, 3 May 2011 16:41:57 +0000 (10:41 -0600)
committerEric Blake <eblake@redhat.com>
Tue, 3 May 2011 17:00:25 +0000 (11:00 -0600)
Clang detected that vol-download will call unlink(NULL) if there
is a parse error during option parsing.  Also, mingw doesn't like
unlinking an open file.

* tools/virsh.c (cmdVolDownload): Only unlink file if created.

tools/virsh.c

index 3d4ed2ffe6c20c4755d635d7a60bac5afcb247f4..5d8b0250170f3664dcd2dfaf7254ca1444c19c5d 100644 (file)
@@ -7259,10 +7259,10 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd)
     virStreamPtr st = NULL;
     const char *name = NULL;
     unsigned long long offset = 0, length = 0;
-    bool created = true;
+    bool created = false;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
-        goto cleanup;
+        return false;
 
     if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
         vshError(ctl, _("Unable to parse integer"));
@@ -7283,12 +7283,13 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd)
     }
 
     if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
-        created = false;
         if (errno != EEXIST ||
             (fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) {
             vshError(ctl, _("cannot create %s"), file);
             goto cleanup;
         }
+    } else {
+        created = true;
     }
 
     st = virStreamNew(ctl->conn, 0);
@@ -7316,13 +7317,13 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
 cleanup:
+    VIR_FORCE_CLOSE(fd);
     if (ret == false && created)
         unlink(file);
     if (vol)
         virStorageVolFree(vol);
     if (st)
         virStreamFree(st);
-    VIR_FORCE_CLOSE(fd);
     return ret;
 }