From c82cf5c08617c947b34eb490d1714729103e3379 Mon Sep 17 00:00:00 2001
From: Andrew Jones <drjones@redhat.com>
Date: Mon, 10 Feb 2020 17:33:57 +0000
Subject: [PATCH 17/18] target/arm/monitor: query-cpu-model-expansion crashed
 qemu when using machine type none
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

RH-Author: Andrew Jones <drjones@redhat.com>
Message-id: <20200210173358.16896-2-drjones@redhat.com>
Patchwork-id: 93773
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 1/2] target/arm/monitor: query-cpu-model-expansion crashed qemu when using machine type none
Bugzilla: 1801320
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
RH-Acked-by: Gavin Shan <gshan@redhat.com>
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1801320

Author: Liang Yan <lyan@suse.com>
Date:   Fri, 07 Feb 2020 14:04:21 +0000

    target/arm/monitor: query-cpu-model-expansion crashed qemu when using machine type none

    Commit e19afd566781 mentioned that target-arm only supports queryable
    cpu models 'max', 'host', and the current type when KVM is in use.
    The logic works well until using machine type none.

    For machine type none, cpu_type will be null if cpu option is not
    set by command line, strlen(cpu_type) will terminate process.
    So We add a check above it.

    This won't affect i386 and s390x since they do not use current_cpu.

    Signed-off-by: Liang Yan <lyan@suse.com>
    Message-id: 20200203134251.12986-1-lyan@suse.com
    Reviewed-by: Andrew Jones <drjones@redhat.com>
    Tested-by: Andrew Jones <drjones@redhat.com>
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

(cherry picked from commit 0999a4ba8718aa96105b978d3567fc7e90244c7e)
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
---
 target/arm/monitor.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/target/arm/monitor.c b/target/arm/monitor.c
index 9725dff..c2dc790 100644
--- a/target/arm/monitor.c
+++ b/target/arm/monitor.c
@@ -137,17 +137,20 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
     }
 
     if (kvm_enabled()) {
-        const char *cpu_type = current_machine->cpu_type;
-        int len = strlen(cpu_type) - strlen(ARM_CPU_TYPE_SUFFIX);
         bool supported = false;
 
         if (!strcmp(model->name, "host") || !strcmp(model->name, "max")) {
             /* These are kvmarm's recommended cpu types */
             supported = true;
-        } else if (strlen(model->name) == len &&
-                   !strncmp(model->name, cpu_type, len)) {
-            /* KVM is enabled and we're using this type, so it works. */
-            supported = true;
+        } else if (current_machine->cpu_type) {
+            const char *cpu_type = current_machine->cpu_type;
+            int len = strlen(cpu_type) - strlen(ARM_CPU_TYPE_SUFFIX);
+
+            if (strlen(model->name) == len &&
+                !strncmp(model->name, cpu_type, len)) {
+                /* KVM is enabled and we're using this type, so it works. */
+                supported = true;
+            }
         }
         if (!supported) {
             error_setg(errp, "We cannot guarantee the CPU type '%s' works "
-- 
1.8.3.1

