[PATCH] tests: Check for virQEMUDriverConfigNew return value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The function may return NULL if something went wrong. In some places
in the tests we are not checking the return value rather than
accessing the pointer directly resulting in SIGSEGV.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tests/domaincapstest.c  | 3 +++
 tests/qemuhotplugtest.c | 3 ++-
 tests/qemuxmlnstest.c   | 3 ++-
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c
index 70d2ef3..fa931f6 100644
--- a/tests/domaincapstest.c
+++ b/tests/domaincapstest.c
@@ -212,46 +212,49 @@ static int
 mymain(void)
 {
     int ret = 0;
 
 #define DO_TEST(Filename, Emulatorbin, Machine, Arch, Type, ...)                    \
     do {                                                                            \
         struct test_virDomainCapsFormatData data = {.filename = Filename,           \
             .emulatorbin = Emulatorbin, .machine = Machine, .arch = Arch,           \
             .type = Type, __VA_ARGS__};                                             \
         if (virtTestRun(Filename, test_virDomainCapsFormat, &data) < 0)             \
             ret = -1;                                                               \
     } while (0)
 
     DO_TEST("basic", "/bin/emulatorbin", "my-machine-type",
             VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_UML);
     DO_TEST("full", "/bin/emulatorbin", "my-machine-type",
             VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_KVM, .fillFunc = fillAll);
 
 #ifdef WITH_QEMU
 
     virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false);
 
+    if (!cfg)
+        return EXIT_FAILURE;
+
 # define DO_TEST_QEMU(Filename, QemuCapsFile, Emulatorbin, Machine, Arch, Type, ...)    \
     do {                                                                                \
         const char *capsPath = abs_srcdir "/qemucapabilitiesdata/" QemuCapsFile ".caps";    \
         virQEMUCapsPtr qemuCaps = qemuTestParseCapabilities(capsPath);                  \
         struct fillQemuCapsData fillData = {.qemuCaps = qemuCaps, .cfg = cfg};          \
         struct test_virDomainCapsFormatData data = {.filename = Filename,               \
             .emulatorbin = Emulatorbin, .machine = Machine, .arch = Arch,               \
             .type = Type, .fillFunc = fillQemuCaps, .opaque = &fillData};               \
         if (!qemuCaps) {                                                                \
             fprintf(stderr, "Unable to build qemu caps from %s\n", capsPath);           \
             ret = -1;                                                                   \
         } else if (virtTestRun(Filename, test_virDomainCapsFormat, &data) < 0)          \
             ret = -1;                                                                   \
     } while (0)
 
     DO_TEST_QEMU("qemu_1.6.50-1", "caps_1.6.50-1", "/usr/bin/qemu-system-x86_64",
                  "pc-1.2",  VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_KVM);
 
     virObjectUnref(cfg);
 #endif /* WITH_QEMU */
 
     return ret;
 }
 
diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c
index 1c18dbb..12a7f71 100644
--- a/tests/qemuhotplugtest.c
+++ b/tests/qemuhotplugtest.c
@@ -333,165 +333,166 @@ static int
 mymain(void)
 {
     int ret = 0;
     struct qemuHotplugTestData data = {0};
     virSecurityManagerPtr mgr;
 
 #if !WITH_YAJL
     fputs("libvirt not compiled with yajl, skipping this test\n", stderr);
     return EXIT_AM_SKIP;
 #endif
 
     if (virThreadInitialize() < 0 ||
         !(driver.caps = testQemuCapsInit()) ||
         !(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver)))
         return EXIT_FAILURE;
 
     virEventRegisterDefaultImpl();
 
-    driver.config = virQEMUDriverConfigNew(false);
+    if (!(driver.config = virQEMUDriverConfigNew(false)))
+        return EXIT_FAILURE;
     VIR_FREE(driver.config->spiceListen);
     VIR_FREE(driver.config->vncListen);
     /* some dummy values from 'config file' */
     if (VIR_STRDUP_QUIET(driver.config->spicePassword, "123456") < 0)
         return EXIT_FAILURE;
 
     if (!(driver.domainEventState = virObjectEventStateNew()))
         return EXIT_FAILURE;
 
     driver.lockManager = virLockManagerPluginNew("nop", "qemu",
                                                  driver.config->configBaseDir,
                                                  0);
     if (!driver.lockManager)
         return EXIT_FAILURE;
 
     if (!(mgr = virSecurityManagerNew("none", "qemu", false, false, false)))
         return EXIT_FAILURE;
     if (!(driver.securityManager = virSecurityManagerNewStack(mgr)))
         return EXIT_FAILURE;
 
     /* wait only 100ms for DEVICE_DELETED event */
     qemuDomainRemoveDeviceWaitTime = 100;
 
 #define DO_TEST(file, ACTION, dev, event, fial, kep, ...)                   \
     do {                                                                    \
         const char *my_mon[] = { __VA_ARGS__, NULL};                        \
         const char *name = file " " #ACTION " " dev;                        \
         data.action = ACTION;                                               \
         data.domain_filename = file;                                        \
         data.device_filename = dev;                                         \
         data.fail = fial;                                                   \
         data.mon = my_mon;                                                  \
         data.keep = kep;                                                    \
         data.deviceDeletedEvent = event;                                    \
         if (virtTestRun(name, testQemuHotplug, &data) < 0)                  \
             ret = -1;                                                       \
     } while (0)
 
 #define DO_TEST_ATTACH(file, dev, fial, kep, ...)                           \
     DO_TEST(file, ATTACH, dev, false, fial, kep, __VA_ARGS__)
 
 #define DO_TEST_DETACH(file, dev, fial, kep, ...)                           \
     DO_TEST(file, DETACH, dev, false, fial, kep, __VA_ARGS__)
 
 #define DO_TEST_ATTACH_EVENT(file, dev, fial, kep, ...)                     \
     DO_TEST(file, ATTACH, dev, true, fial, kep, __VA_ARGS__)
 
 #define DO_TEST_DETACH_EVENT(file, dev, fial, kep, ...)                     \
     DO_TEST(file, DETACH, dev, true, fial, kep, __VA_ARGS__)
 
 #define DO_TEST_UPDATE(file, dev, fial, kep, ...)                           \
     DO_TEST(file, UPDATE, dev, false, fial, kep, __VA_ARGS__)
 
 
 #define QMP_OK      "{\"return\": {}}"
 #define HMP(msg)    "{\"return\": \"" msg "\"}"
 
 #define QMP_DEVICE_DELETED(dev) \
     "{"                                                     \
     "    \"timestamp\": {"                                  \
     "        \"seconds\": 1374137171,"                      \
     "        \"microseconds\": 2659"                        \
     "    },"                                                \
     "    \"event\": \"DEVICE_DELETED\","                    \
     "    \"data\": {"                                       \
     "        \"device\": \"" dev "\","                      \
     "        \"path\": \"/machine/peripheral/" dev "\""     \
     "    }"                                                 \
     "}\r\n"
 
     DO_TEST_UPDATE("graphics-spice", "graphics-spice-nochange", false, false, NULL);
     DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-nochange", false, false,
                    "set_password", QMP_OK, "expire_password", QMP_OK);
     DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-password", false, false,
                    "set_password", QMP_OK, "expire_password", QMP_OK);
     DO_TEST_UPDATE("graphics-spice", "graphics-spice-listen", true, false, NULL);
     DO_TEST_UPDATE("graphics-spice-listen-network", "graphics-spice-listen-network", false, false,
                    "set_password", QMP_OK, "expire_password", QMP_OK);
     /* Strange huh? Currently, only graphics can be updated :-P */
     DO_TEST_UPDATE("disk-cdrom", "disk-cdrom-nochange", true, false, NULL);
 
     DO_TEST_ATTACH("console-compat-2", "console-virtio", false, true,
                    "chardev-add", "{\"return\": {\"pty\": \"/dev/pts/26\"}}",
                    "device_add", QMP_OK);
 
     DO_TEST_DETACH("console-compat-2", "console-virtio", false, false,
                    "device_del", QMP_OK,
                    "chardev-remove", QMP_OK);
 
     DO_TEST_ATTACH("hotplug-base", "disk-virtio", false, true,
                    "human-monitor-command", HMP("OK\\r\\n"),
                    "device_add", QMP_OK);
     DO_TEST_DETACH("hotplug-base", "disk-virtio", false, false,
                    "device_del", QMP_OK,
                    "human-monitor-command", HMP(""));
 
     DO_TEST_ATTACH_EVENT("hotplug-base", "disk-virtio", false, true,
                          "human-monitor-command", HMP("OK\\r\\n"),
                          "device_add", QMP_OK);
     DO_TEST_DETACH("hotplug-base", "disk-virtio", true, true,
                    "device_del", QMP_OK,
                    "human-monitor-command", HMP(""));
     DO_TEST_DETACH("hotplug-base", "disk-virtio", false, false,
                    "device_del", QMP_DEVICE_DELETED("virtio-disk4") QMP_OK,
                    "human-monitor-command", HMP(""));
 
     DO_TEST_ATTACH("hotplug-base", "disk-usb", false, true,
                    "human-monitor-command", HMP("OK\\r\\n"),
                    "device_add", QMP_OK);
     DO_TEST_DETACH("hotplug-base", "disk-usb", false, false,
                    "device_del", QMP_OK,
                    "human-monitor-command", HMP(""));
 
     DO_TEST_ATTACH_EVENT("hotplug-base", "disk-usb", false, true,
                          "human-monitor-command", HMP("OK\\r\\n"),
                          "device_add", QMP_OK);
     DO_TEST_DETACH("hotplug-base", "disk-usb", true, true,
                    "device_del", QMP_OK,
                    "human-monitor-command", HMP(""));
     DO_TEST_DETACH("hotplug-base", "disk-usb", false, false,
                    "device_del", QMP_DEVICE_DELETED("usb-disk16") QMP_OK,
                    "human-monitor-command", HMP(""));
 
     DO_TEST_ATTACH("hotplug-base", "disk-scsi", false, true,
                    "human-monitor-command", HMP("OK\\r\\n"),
                    "device_add", QMP_OK);
     DO_TEST_DETACH("hotplug-base", "disk-scsi", false, false,
                    "device_del", QMP_OK,
                    "human-monitor-command", HMP(""));
 
     DO_TEST_ATTACH_EVENT("hotplug-base", "disk-scsi", false, true,
                          "human-monitor-command", HMP("OK\\r\\n"),
                          "device_add", QMP_OK);
     DO_TEST_DETACH("hotplug-base", "disk-scsi", true, true,
                    "device_del", QMP_OK,
                    "human-monitor-command", HMP(""));
     DO_TEST_DETACH("hotplug-base", "disk-scsi", false, false,
                    "device_del", QMP_DEVICE_DELETED("scsi0-0-0-5") QMP_OK,
                    "human-monitor-command", HMP(""));
 
     virObjectUnref(driver.caps);
     virObjectUnref(driver.xmlopt);
     virObjectUnref(driver.config);
     return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
diff --git a/tests/qemuxmlnstest.c b/tests/qemuxmlnstest.c
index 947aa9c..4d665da 100644
--- a/tests/qemuxmlnstest.c
+++ b/tests/qemuxmlnstest.c
@@ -205,62 +205,63 @@ static int
 mymain(void)
 {
     int ret = 0;
     bool json = false;
 
     abs_top_srcdir = getenv("abs_top_srcdir");
     if (!abs_top_srcdir)
         abs_top_srcdir = abs_srcdir "/..";
 
-    driver.config = virQEMUDriverConfigNew(false);
+    if (!(driver.config = virQEMUDriverConfigNew(false)))
+        return EXIT_FAILURE;
     if ((driver.caps = testQemuCapsInit()) == NULL)
         return EXIT_FAILURE;
     if (!(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver)))
         return EXIT_FAILURE;
 
 # define DO_TEST_FULL(name, migrateFrom, migrateFd, expectError, ...)   \
     do {                                                                \
         struct testInfo info = {                                        \
             name, NULL, migrateFrom, migrateFd, json, expectError       \
         };                                                              \
         if (!(info.extraFlags = virQEMUCapsNew()))                      \
             return EXIT_FAILURE;                                        \
         virQEMUCapsSetList(info.extraFlags, __VA_ARGS__, QEMU_CAPS_LAST);\
         if (virtTestRun("QEMU XML-2-ARGV " name,                        \
                         testCompareXMLToArgvHelper, &info) < 0)         \
             ret = -1;                                                   \
         virObjectUnref(info.extraFlags);                                \
     } while (0)
 
 # define DO_TEST(name, expectError, ...)                                \
     DO_TEST_FULL(name, NULL, -1, expectError, __VA_ARGS__)
 
 # define NONE QEMU_CAPS_LAST
 
     /* Unset or set all envvars here that are copied in qemudBuildCommandLine
      * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
      * values for these envvars */
     setenv("PATH", "/bin", 1);
     setenv("USER", "test", 1);
     setenv("LOGNAME", "test", 1);
     setenv("HOME", "/home/test", 1);
     unsetenv("TMPDIR");
     unsetenv("LD_PRELOAD");
     unsetenv("LD_LIBRARY_PATH");
     unsetenv("QEMU_AUDIO_DRV");
     unsetenv("SDL_AUDIODRIVER");
 
     DO_TEST("qemu-ns-domain", false, NONE);
     DO_TEST("qemu-ns-domain-ns0", false, NONE);
     DO_TEST("qemu-ns-domain-commandline", false, NONE);
     DO_TEST("qemu-ns-domain-commandline-ns0", false, NONE);
     DO_TEST("qemu-ns-commandline", false, NONE);
     DO_TEST("qemu-ns-commandline-ns0", false, NONE);
     DO_TEST("qemu-ns-commandline-ns1", false, NONE);
 
     virObjectUnref(driver.config);
     virObjectUnref(driver.caps);
     virObjectUnref(driver.xmlopt);
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-- 
2.0.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]