提交 44699b32 编写于 作者: E Eric Blake

virsh: avoid null pointer dereference

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.
上级 1164e1a2
...@@ -7259,10 +7259,10 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd) ...@@ -7259,10 +7259,10 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd)
virStreamPtr st = NULL; virStreamPtr st = NULL;
const char *name = NULL; const char *name = NULL;
unsigned long long offset = 0, length = 0; unsigned long long offset = 0, length = 0;
bool created = true; bool created = false;
if (!vshConnectionUsability(ctl, ctl->conn)) if (!vshConnectionUsability(ctl, ctl->conn))
goto cleanup; return false;
if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) { if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
vshError(ctl, _("Unable to parse integer")); vshError(ctl, _("Unable to parse integer"));
...@@ -7283,12 +7283,13 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd) ...@@ -7283,12 +7283,13 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd)
} }
if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
created = false;
if (errno != EEXIST || if (errno != EEXIST ||
(fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) { (fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) {
vshError(ctl, _("cannot create %s"), file); vshError(ctl, _("cannot create %s"), file);
goto cleanup; goto cleanup;
} }
} else {
created = true;
} }
st = virStreamNew(ctl->conn, 0); st = virStreamNew(ctl->conn, 0);
...@@ -7316,13 +7317,13 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd) ...@@ -7316,13 +7317,13 @@ cmdVolDownload (vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
VIR_FORCE_CLOSE(fd);
if (ret == false && created) if (ret == false && created)
unlink(file); unlink(file);
if (vol) if (vol)
virStorageVolFree(vol); virStorageVolFree(vol);
if (st) if (st)
virStreamFree(st); virStreamFree(st);
VIR_FORCE_CLOSE(fd);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册