]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: Fix segfault when host CPU is empty
authorJiri Denemark <jdenemar@redhat.com>
Wed, 15 Feb 2012 11:18:25 +0000 (12:18 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Thu, 16 Feb 2012 09:41:13 +0000 (10:41 +0100)
In case libvirtd cannot detect host CPU model (which may happen if it
runs inside a virtual machine), the daemon is likely to segfault when
starting a new qemu domain. It segfaults when domain XML asks for host
(either model or passthrough) CPU or does not ask for any specific CPU
model at all.

src/qemu/qemu_command.c

index 14e414285bf84a82ffe1662022168d76cf406286..5a345042cbc72c7ae8173c1b2acc4c88fce837e9 100644 (file)
@@ -3509,22 +3509,13 @@ qemuBuildCpuArgStr(const struct qemud_driver *driver,
 
     *hasHwVirt = false;
 
-    if (def->cpu &&
-        (def->cpu->mode != VIR_CPU_MODE_CUSTOM || def->cpu->model)) {
-        if (!(cpu = virCPUDefCopy(def->cpu)))
-            goto cleanup;
-        if (cpu->mode != VIR_CPU_MODE_CUSTOM &&
-            !migrating &&
-            cpuUpdate(cpu, host) < 0)
-            goto cleanup;
-    }
-
     if (STREQ(def->os.arch, "i686"))
         default_model = "qemu32";
     else
         default_model = "qemu64";
 
-    if (cpu) {
+    if (def->cpu &&
+        (def->cpu->mode != VIR_CPU_MODE_CUSTOM || def->cpu->model)) {
         virCPUCompareResult cmp;
         const char *preferred;
         int hasSVM;
@@ -3540,6 +3531,14 @@ qemuBuildCpuArgStr(const struct qemud_driver *driver,
             goto cleanup;
         }
 
+        if (!(cpu = virCPUDefCopy(def->cpu)))
+            goto cleanup;
+
+        if (cpu->mode != VIR_CPU_MODE_CUSTOM &&
+            !migrating &&
+            cpuUpdate(cpu, host) < 0)
+            goto cleanup;
+
         cmp = cpuGuestData(host, cpu, &data);
         switch (cmp) {
         case VIR_CPU_COMPARE_INCOMPATIBLE:
@@ -3648,7 +3647,8 @@ qemuBuildCpuArgStr(const struct qemud_driver *driver,
     ret = 0;
 
 cleanup:
-    cpuDataFree(host->arch, data);
+    if (host)
+        cpuDataFree(host->arch, data);
     virCPUDefFree(guest);
     virCPUDefFree(cpu);