Skip to contentSkip to conversation

cpu: Fix crash with empty -cpu option

PATCH Applied Eduardo Habkost 3 participants 6 messages Apr 18, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu: Fix crash with empty -cpu option

Fix the following crash:

  $ qemu-system-x86_64 -cpu ''
  qemu-system-x86_64: qom/cpu.c:291: cpu_class_by_name: \
      Assertion `cpu_model && cc->class_by_name' failed.

Regression test script included.

Fixes commit 99193d8f2ef5 ("cpu: drop unnecessary NULL check and cpu_common_class_by_name()")
---
 exec.c                              |  4 ++++
 tests/acceptance/empty_cpu_model.py | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/acceptance/empty_cpu_model.py
exec.c +4 -0
index 1ca95df9d8..d816b38863 100644
@@ -999,6 +999,10 @@ const char *parse_cpu_option(MachineState *machine, const char *cpu_option)
     const char *cpu_type;
 
     model_pieces = g_strsplit(cpu_option, ",", 2);
+    if (!model_pieces[0]) {
+        error_report("-cpu option cannot be empty");
+        exit(1);
+    }
 
     cc = lookup_cpu_class(model_pieces[0], &error_fatal);
     cpu_type = object_class_get_name(OBJECT_CLASS(cc));
tests/acceptance/empty_cpu_model.py new file +19 -0
new file mode 100644
index 0000000000..3f4f663582
@@ -0,0 +1,19 @@
+# Check for crash when using empty -cpu option
+#
+# Copyright (c) 2019 Red Hat, Inc.
+#
+# Author:
+#  Eduardo Habkost <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later.  See the COPYING file in the top-level directory.
+import subprocess
+from avocado_qemu import Test
+
+class EmptyCPUModel(Test):
+    def test(self):
+        cmd = [self.qemu_bin, '-S', '-display', 'none', '-machine', 'none', '-cpu', '']
+        r = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
+        self.assertEquals(r.returncode, 1, "QEMU exit code should be 1")
+        self.assertEquals(r.stdout, b'', "QEMU stdout should be empty")
+        self.assertNotEquals(r.stderr, b'', "QEMU stderr shouldn't be empty")
Signature · 1 line
2.18.0.rc1.1.g3f1ff2140
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 8 lines · Eduardo Habkost

Fix the following crash:

  $ qemu-system-x86_64 -cpu ''
  qemu-system-x86_64: qom/cpu.c:291: cpu_class_by_name: \
      Assertion `cpu_model && cc->class_by_name' failed.

Regression test script included.

Quoted 8 lines
Fixes commit 99193d8f2ef5 ("cpu: drop unnecessary NULL check and cpu_common_class_by_name()")
---
 exec.c                              |  4 ++++
 tests/acceptance/empty_cpu_model.py | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/acceptance/empty_cpu_model.py

Thanks,
Stefano

#2 lore raw
In reply to Eduardo Habkost
Quoted 21 lines · On Thu, 18 Apr 2019 00:45:01 -0300 Eduardo Habkost <[email protected]> wrote:

Fix the following crash:

  $ qemu-system-x86_64 -cpu ''
  qemu-system-x86_64: qom/cpu.c:291: cpu_class_by_name: \
      Assertion `cpu_model && cc->class_by_name' failed.

Regression test script included.

Fixes commit 99193d8f2ef5 ("cpu: drop unnecessary NULL check and cpu_common_class_by_name()")
---
 exec.c                              |  4 ++++
 tests/acceptance/empty_cpu_model.py | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/acceptance/empty_cpu_model.py
exec.c +0 -0
index 1ca95df9d8..d816b38863 100644

exec.c const char *parse_cpu_option(MachineState *machine, const char *cpu_option) · line 1004

Quoted 6 lines
exec.c +3 -0
@@ -999,6 +999,10 @@ const char *parse_cpu_option(MachineState *machine, const char *cpu_option)
     const char *cpu_type;
 
     model_pieces = g_strsplit(cpu_option, ",", 2);
+    if (!model_pieces[0]) {
+        error_report("-cpu option cannot be empty");
+        exit(1);

s/1/EXIT_FAILURE/

Quoted 29 lines
exec.c +1 -0
+    }
 
     cc = lookup_cpu_class(model_pieces[0], &error_fatal);
     cpu_type = object_class_get_name(OBJECT_CLASS(cc));
tests/acceptance/empty_cpu_model.py new file +19 -0
new file mode 100644
index 0000000000..3f4f663582
@@ -0,0 +1,19 @@
+# Check for crash when using empty -cpu option
+#
+# Copyright (c) 2019 Red Hat, Inc.
+#
+# Author:
+#  Eduardo Habkost <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later.  See the COPYING file in the top-level directory.
+import subprocess
+from avocado_qemu import Test
+
+class EmptyCPUModel(Test):
+    def test(self):
+        cmd = [self.qemu_bin, '-S', '-display', 'none', '-machine', 'none', '-cpu', '']
+        r = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
+        self.assertEquals(r.returncode, 1, "QEMU exit code should be 1")
+        self.assertEquals(r.stdout, b'', "QEMU stdout should be empty")
+        self.assertNotEquals(r.stderr, b'', "QEMU stderr shouldn't be empty")
#3 lore raw
Quoted 26 lines · Igor Mammedov
Quoted 23 lines · On Thu, 18 Apr 2019 00:45:01 -0300 Eduardo Habkost <[email protected]> wrote:

Fix the following crash:

  $ qemu-system-x86_64 -cpu ''
  qemu-system-x86_64: qom/cpu.c:291: cpu_class_by_name: \
      Assertion `cpu_model && cc->class_by_name' failed.

Regression test script included.

Fixes commit 99193d8f2ef5 ("cpu: drop unnecessary NULL check and cpu_common_class_by_name()")
---
 exec.c                              |  4 ++++
 tests/acceptance/empty_cpu_model.py | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/acceptance/empty_cpu_model.py
exec.c +0 -0
index 1ca95df9d8..d816b38863 100644
@@ -999,6 +999,10 @@ const char *parse_cpu_option(MachineState *machine, const char *cpu_option)
     const char *cpu_type;
 
Quoted 6 lines

exec.c const char *parse_cpu_option(MachineState *machine, const char *cpu_option) · line 1004

Quoted 4 lines
exec.c +3 -0
     model_pieces = g_strsplit(cpu_option, ",", 2);
+    if (!model_pieces[0]) {
+        error_report("-cpu option cannot be empty");
+        exit(1);

s/1/EXIT_FAILURE/

Why?

There are 753 instances of "exit(1)" in the tree, and 171 instances of "exit(EXIT_FAILURE)".

Signature · 1 line
Eduardo
#4 lore raw
Quoted 27 lines · On Thu, 18 Apr 2019 10:23:29 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 26 lines · Igor Mammedov
Quoted 23 lines · On Thu, 18 Apr 2019 00:45:01 -0300 Eduardo Habkost <[email protected]> wrote:

Fix the following crash:

  $ qemu-system-x86_64 -cpu ''
  qemu-system-x86_64: qom/cpu.c:291: cpu_class_by_name: \
      Assertion `cpu_model && cc->class_by_name' failed.

Regression test script included.

Fixes commit 99193d8f2ef5 ("cpu: drop unnecessary NULL check and cpu_common_class_by_name()")
---
 exec.c                              |  4 ++++
 tests/acceptance/empty_cpu_model.py | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/acceptance/empty_cpu_model.py
exec.c +0 -0
index 1ca95df9d8..d816b38863 100644
@@ -999,6 +999,10 @@ const char *parse_cpu_option(MachineState *machine, const char *cpu_option)
     const char *cpu_type;
 
Quoted 8 lines
Quoted 6 lines

exec.c const char *parse_cpu_option(MachineState *machine, const char *cpu_option) · line 1004

Quoted 4 lines
exec.c +3 -0
     model_pieces = g_strsplit(cpu_option, ",", 2);
+    if (!model_pieces[0]) {
+        error_report("-cpu option cannot be empty");
+        exit(1);  

s/1/EXIT_FAILURE/

Why?

it's more descriptive in general

Quoted 2 lines

There are 753 instances of "exit(1)" in the tree, and 171 instances of "exit(EXIT_FAILURE)".

Bad example in past is probably not a good justification, especially when adding new code (but I'm not going to argue about it).

author Applied
#5 lore raw
In reply to Eduardo Habkost
Quoted 10 lines · Eduardo Habkost

Fix the following crash:

  $ qemu-system-x86_64 -cpu ''
  qemu-system-x86_64: qom/cpu.c:291: cpu_class_by_name: \
      Assertion `cpu_model && cc->class_by_name' failed.

Regression test script included.

Fixes commit 99193d8f2ef5 ("cpu: drop unnecessary NULL check and cpu_common_class_by_name()")

Queued on machine-next.

Signature · 1 line
Eduardo