[Qemu-devel] [PATCH 2/2] target/i386/monitor.c: check return value of mon_get_cpu before using it

Ziyue Yang posted 2 patches 8 years, 11 months ago
[Qemu-devel] [PATCH 2/2] target/i386/monitor.c: check return value of mon_get_cpu before using it
Posted by Ziyue Yang 8 years, 11 months ago
From: Ziyue Yang <yzylivezh@hotmail.com>

This patch eliminates the segfault caused by accessing CPU that doesn't
exist in hmp command "info lapic", which can be reproduced by

$ qemu-system-x86_64 -nographic -M none -serial none -monitor stdio

and then type "info lapic" into qemu monitor.

Signed-off-by: Ziyue Yang <skiver.cloud.yzy@gmail.com>
---
 target/i386/monitor.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/target/i386/monitor.c b/target/i386/monitor.c
index 468aa073bc..7b96c74a24 100644
--- a/target/i386/monitor.c
+++ b/target/i386/monitor.c
@@ -624,8 +624,11 @@ const MonitorDef *target_monitor_defs(void)
 
 void hmp_info_local_apic(Monitor *mon, const QDict *qdict)
 {
-    x86_cpu_dump_local_apic_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf,
-                                  CPU_DUMP_FPU);
+    CPUState *cs = mon_get_cpu();
+    if (cs) {
+        x86_cpu_dump_local_apic_state(cs, (FILE *)mon, monitor_fprintf,
+                                      CPU_DUMP_FPU);
+    }
 }
 
 void hmp_info_io_apic(Monitor *mon, const QDict *qdict)
-- 
2.11.0


Re: [Qemu-devel] [PATCH 2/2] target/i386/monitor.c: check return value of mon_get_cpu before using it
Posted by Philippe Mathieu-Daudé 8 years, 11 months ago
On 02/17/2017 05:27 AM, Ziyue Yang wrote:
> From: Ziyue Yang <yzylivezh@hotmail.com>
>
> This patch eliminates the segfault caused by accessing CPU that doesn't
> exist in hmp command "info lapic", which can be reproduced by
>
> $ qemu-system-x86_64 -nographic -M none -serial none -monitor stdio
>
> and then type "info lapic" into qemu monitor.
>
> Signed-off-by: Ziyue Yang <skiver.cloud.yzy@gmail.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
>  target/i386/monitor.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/target/i386/monitor.c b/target/i386/monitor.c
> index 468aa073bc..7b96c74a24 100644
> --- a/target/i386/monitor.c
> +++ b/target/i386/monitor.c
> @@ -624,8 +624,11 @@ const MonitorDef *target_monitor_defs(void)
>
>  void hmp_info_local_apic(Monitor *mon, const QDict *qdict)
>  {
> -    x86_cpu_dump_local_apic_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf,
> -                                  CPU_DUMP_FPU);
> +    CPUState *cs = mon_get_cpu();
> +    if (cs) {
> +        x86_cpu_dump_local_apic_state(cs, (FILE *)mon, monitor_fprintf,
> +                                      CPU_DUMP_FPU);
> +    }
>  }
>
>  void hmp_info_io_apic(Monitor *mon, const QDict *qdict)
>