diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2011-08-23 18:02:24 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2011-08-23 18:03:23 +0100 |
commit | 08e77ad8cb4e8ac70d4217ebd5d236eba81645b5 (patch) | |
tree | 86f26da625c4d02c057b6e3a489488e801f18f13 /src | |
parent | fa44536b0ff12102c72ae0337e51d272f0fc2353 (diff) | |
download | libguestfs-08e77ad8cb4e8ac70d4217ebd5d236eba81645b5.tar.gz libguestfs-08e77ad8cb4e8ac70d4217ebd5d236eba81645b5.tar.xz libguestfs-08e77ad8cb4e8ac70d4217ebd5d236eba81645b5.zip |
Coverity: test_qemu: Ensure FILE * is not leaked along error paths.
This refactors the code in test_qemu slightly to ensure that
FILE *fp is not leaked on error paths.
Diffstat (limited to 'src')
-rw-r--r-- | src/launch.c | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/src/launch.c b/src/launch.c index 234f42e4..db76885a 100644 --- a/src/launch.c +++ b/src/launch.c @@ -1058,6 +1058,7 @@ print_qemu_command_line (guestfs_h *g, char **argv) } } +static int test_qemu_cmd (guestfs_h *g, const char *cmd, char **ret); static int read_all (guestfs_h *g, FILE *fp, char **ret); /* Test qemu binary (or wrapper) runs, and do 'qemu -help' and @@ -1072,36 +1073,41 @@ test_qemu (guestfs_h *g) snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -help", g->qemu); - fp = popen (cmd, "r"); /* qemu -help should always work (qemu -version OTOH wasn't * supported by qemu 0.9). If this command doesn't work then it * probably indicates that the qemu binary is missing. */ - if (!fp) { - /* XXX This error is never printed, even if the qemu binary - * doesn't exist. Why? - */ - error: + if (test_qemu_cmd (g, cmd, &g->qemu_help) == -1) { perrorf (g, _("%s: command failed: If qemu is located on a non-standard path, try setting the LIBGUESTFS_QEMU environment variable."), cmd); return -1; } - if (read_all (g, fp, &g->qemu_help) == -1) - goto error; - - if (pclose (fp) == -1) - goto error; - snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -version 2>/dev/null", g->qemu); + /* Intentionally ignore errors from qemu -version. */ + ignore_value (test_qemu_cmd (g, cmd, &g->qemu_version)); + + return 0; +} + +static int +test_qemu_cmd (guestfs_h *g, const char *cmd, char **ret) +{ + FILE *fp; + fp = popen (cmd, "r"); - if (fp) { - /* Intentionally ignore errors. */ - read_all (g, fp, &g->qemu_version); + if (fp == NULL) + return -1; + + if (read_all (g, fp, ret) == -1) { pclose (fp); + return -1; } + if (pclose (fp) == -1) + return -1; + return 0; } |