[libvirt] [PATCH 02/14] conf, qemu: Use type-aware switches where possible

Andrea Bolognani posted 14 patches 7 years, 6 months ago
There is a newer version of this series
[libvirt] [PATCH 02/14] conf, qemu: Use type-aware switches where possible
Posted by Andrea Bolognani 7 years, 6 months ago
The compiler can warn us if we add a value to the
virDomainChrSerialTargetType enumeration but forget to handle
it properly in the code. Let's take advantage of that.

This commit is best viewed with 'git diff -w'.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
---
 src/conf/domain_conf.c  | 47 ++++++++++++++++++++++++++++++-----------------
 src/qemu/qemu_command.c |  7 ++++++-
 2 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 62d0a1683..9da8dd646 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -4035,26 +4035,39 @@ virDomainDefAddConsoleCompat(virDomainDefPtr def)
             def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
         }
     } else if (def->os.type == VIR_DOMAIN_OSTYPE_HVM && def->nserials > 0 &&
-               def->serials[0]->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
-               def->serials[0]->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_ISA) {
-        /* Create a stub console to match the serial port.
-         * console[0] either does not exist
-         *                or has a different type than SERIAL or NONE.
-         */
-        virDomainChrDefPtr chr;
-        if (!(chr = virDomainChrDefNew(NULL)))
-            return -1;
+               def->serials[0]->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL) {
 
-        if (VIR_INSERT_ELEMENT(def->consoles,
-                               0,
-                               def->nconsoles,
-                               chr) < 0) {
-            virDomainChrDefFree(chr);
-            return -1;
+        switch ((virDomainChrSerialTargetType) def->serials[0]->targetType) {
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_ISA: {
+
+            /* Create a stub console to match the serial port.
+             * console[0] either does not exist
+             *                or has a different type than SERIAL or NONE.
+             */
+            virDomainChrDefPtr chr;
+            if (!(chr = virDomainChrDefNew(NULL)))
+                return -1;
+
+            if (VIR_INSERT_ELEMENT(def->consoles,
+                                   0,
+                                   def->nconsoles,
+                                   chr) < 0) {
+                virDomainChrDefFree(chr);
+                return -1;
+            }
+
+            def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
+            def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
+
+            break;
         }
 
-        def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
-        def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_PCI:
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB:
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_LAST:
+            /* Nothing to do */
+            break;
+        }
     }
 
     return 0;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 4c05a5f66..010c2992f 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -10206,7 +10206,7 @@ qemuBuildSerialChrDeviceStr(char **deviceStr,
                               serial->info.alias);
         }
     } else {
-        switch (serial->targetType) {
+        switch ((virDomainChrSerialTargetType) serial->targetType) {
         case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB:
             if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_USB_SERIAL)) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
@@ -10245,6 +10245,11 @@ qemuBuildSerialChrDeviceStr(char **deviceStr,
                 goto error;
             }
             break;
+
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_LAST:
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Invalid target type for serial device"));
+            goto error;
         }
 
         virBufferAsprintf(&cmd, "%s,chardev=char%s,id=%s",
-- 
2.13.6

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 02/14] conf, qemu: Use type-aware switches where possible
Posted by Pavel Hrdina 7 years, 5 months ago
On Wed, Nov 15, 2017 at 12:50:05PM +0100, Andrea Bolognani wrote:
> The compiler can warn us if we add a value to the
> virDomainChrSerialTargetType enumeration but forget to handle
> it properly in the code. Let's take advantage of that.
> 
> This commit is best viewed with 'git diff -w'.

This sentence should be placed under "---" as a note and not part of
the commit message.

> 
> Signed-off-by: Andrea Bolognani <abologna@redhat.com>
> ---
>  src/conf/domain_conf.c  | 47 ++++++++++++++++++++++++++++++-----------------
>  src/qemu/qemu_command.c |  7 ++++++-
>  2 files changed, 36 insertions(+), 18 deletions(-)

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 02/14] conf, qemu: Use type-aware switches where possible
Posted by Andrea Bolognani 7 years, 5 months ago
On Thu, 2017-11-16 at 11:27 +0100, Pavel Hrdina wrote:
> On Wed, Nov 15, 2017 at 12:50:05PM +0100, Andrea Bolognani wrote:
> > The compiler can warn us if we add a value to the
> > virDomainChrSerialTargetType enumeration but forget to handle
> > it properly in the code. Let's take advantage of that.
> > 
> > This commit is best viewed with 'git diff -w'.
> 
> This sentence should be placed under "---" as a note and not part of
> the commit message.

I thought that hint could be useful to people looking at the commit
a few years down the line just as it's useful to the reviewer right
now, but I can leave it out if you prefer :)

-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 02/14] conf, qemu: Use type-aware switches where possible
Posted by Pavel Hrdina 7 years, 5 months ago
On Thu, Nov 16, 2017 at 01:01:57PM +0100, Andrea Bolognani wrote:
> On Thu, 2017-11-16 at 11:27 +0100, Pavel Hrdina wrote:
> > On Wed, Nov 15, 2017 at 12:50:05PM +0100, Andrea Bolognani wrote:
> > > The compiler can warn us if we add a value to the
> > > virDomainChrSerialTargetType enumeration but forget to handle
> > > it properly in the code. Let's take advantage of that.
> > > 
> > > This commit is best viewed with 'git diff -w'.
> > 
> > This sentence should be placed under "---" as a note and not part of
> > the commit message.
> 
> I thought that hint could be useful to people looking at the commit
> a few years down the line just as it's useful to the reviewer right
> now, but I can leave it out if you prefer :)

I'm used to putting it as note, but on the other hand it might be
useful to keep it in commit message.  Feel free to leave it there :).

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