From df983d19946fec697db2c502d7a6bb36ff0d2df5 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Mon, 11 Feb 2013 13:18:26 +0000 Subject: [PATCH] test-tool: Don't call guestfs_set_qemu before guestfs handle is initialized (RHBZ#909836). Because of evolution of the code, if the user used the --qemu or --qemudir options, libguestfs-test-tool would segfault because guestfs_set_qemu was being called before the guestfs handle was opened. Change the code so this doesn't happen, and also remove the global 'g' variable to make the code a bit more robust. Bug found by Amit Shah. --- test-tool/test-tool.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/test-tool/test-tool.c b/test-tool/test-tool.c index 140a90586..63f92b5bb 100644 --- a/test-tool/test-tool.c +++ b/test-tool/test-tool.c @@ -44,10 +44,9 @@ static int timeout = DEFAULT_TIMEOUT; static char tmpf[] = P_tmpdir "/libguestfs-test-tool-sda-XXXXXX"; -static guestfs_h *g; static void make_files (void); -static void set_qemu (const char *path, int use_wrapper); +static void set_qemu (guestfs_h *g, const char *path, int use_wrapper); static void usage (void) @@ -91,6 +90,9 @@ main (int argc, char *argv[]) int i; struct guestfs_version *vers; char *p; + guestfs_h *g; + char *qemu = NULL; + int qemu_use_wrapper; for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -98,10 +100,14 @@ main (int argc, char *argv[]) switch (c) { case 0: /* options which are long only */ - if (STREQ (long_options[option_index].name, "qemu")) - set_qemu (optarg, 0); - else if (STREQ (long_options[option_index].name, "qemudir")) - set_qemu (optarg, 1); + if (STREQ (long_options[option_index].name, "qemu")) { + qemu = optarg; + qemu_use_wrapper = 0; + } + else if (STREQ (long_options[option_index].name, "qemudir")) { + qemu = optarg; + qemu_use_wrapper = 1; + } else { fprintf (stderr, _("libguestfs-test-tool: unknown long option: %s (%d)\n"), @@ -176,6 +182,9 @@ main (int argc, char *argv[]) } guestfs_set_verbose (g, 1); + if (qemu) + set_qemu (g, qemu, qemu_use_wrapper); + make_files (); printf ("===== Test starts here =====\n"); @@ -316,7 +325,7 @@ cleanup_wrapper (void) * a wrapper shell script. */ static void -set_qemu (const char *path, int use_wrapper) +set_qemu (guestfs_h *g, const char *path, int use_wrapper) { char *buffer; struct stat statbuf;