[libvirt] [PATCH 3/6] conf: Make switch statements more strict

Andrea Bolognani posted 6 patches 4 years, 3 months ago

[libvirt] [PATCH 3/6] conf: Make switch statements more strict

Posted by Andrea Bolognani 4 years, 3 months ago
When switching over the values in the virDomainControllerModelPCI
enumeration, make sure the proper cast is in place so that the
compiler can warn us when the coverage is not exaustive.

For the same reason, remove the 'default' case from one of the
existing switch statements.
---
 src/conf/domain_addr.c |  4 ++--
 src/conf/domain_conf.c | 12 +++++++++++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c
index 519cc6b..933c2fe 100644
--- a/src/conf/domain_addr.c
+++ b/src/conf/domain_addr.c
@@ -344,9 +344,9 @@ virDomainPCIAddressBusSetModel(virDomainPCIAddressBusPtr bus,
         bus->maxSlot = VIR_PCI_ADDRESS_SLOT_LAST;
         break;
 
-    default:
+    case VIR_DOMAIN_CONTROLLER_MODEL_PCI_LAST:
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Invalid PCI controller model %d"), model);
+                       _("PCI controller model was not set correctly"));
         return -1;
     }
 
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 79bdbdf..3ac8467 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8834,7 +8834,7 @@ virDomainControllerDefParseXML(xmlNodePtr node,
         break;
     }
     case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
-        switch (def->model) {
+        switch ((virDomainControllerModelPCI) def->model) {
         case VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT:
         case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT: {
             unsigned long long bytes;
@@ -8859,6 +8859,16 @@ virDomainControllerDefParseXML(xmlNodePtr node,
                 def->opts.pciopts.pcihole64 = true;
             def->opts.pciopts.pcihole64size = VIR_DIV_UP(bytes, 1024);
         }
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCI_BRIDGE:
+        case VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE:
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT_PORT:
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_SWITCH_UPSTREAM_PORT:
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_SWITCH_DOWNSTREAM_PORT:
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCI_EXPANDER_BUS:
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_EXPANDER_BUS:
+        case VIR_DOMAIN_CONTROLLER_MODEL_PCI_LAST:
+            /* Other controller models don't require extra checks */
+            break;
         }
         if (modelName &&
             (def->opts.pciopts.modelName
-- 
2.7.4

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

Re: [libvirt] [PATCH 3/6] conf: Make switch statements more strict

Posted by Laine Stump 4 years, 3 months ago
On 02/21/2017 02:57 PM, Andrea Bolognani wrote:
> When switching over the values in the virDomainControllerModelPCI
> enumeration, make sure the proper cast is in place so that the
> compiler can warn us when the coverage is not exaustive.
>
> For the same reason, remove the 'default' case from one of the
> existing switch statements.
> ---
>   src/conf/domain_addr.c |  4 ++--
>   src/conf/domain_conf.c | 12 +++++++++++-
>   2 files changed, 13 insertions(+), 3 deletions(-)
>
> diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c
> index 519cc6b..933c2fe 100644
> --- a/src/conf/domain_addr.c
> +++ b/src/conf/domain_addr.c
> @@ -344,9 +344,9 @@ virDomainPCIAddressBusSetModel(virDomainPCIAddressBusPtr bus,
>           bus->maxSlot = VIR_PCI_ADDRESS_SLOT_LAST;
>           break;
>   
> -    default:
> +    case VIR_DOMAIN_CONTROLLER_MODEL_PCI_LAST:
>           virReportError(VIR_ERR_INTERNAL_ERROR,
> -                       _("Invalid PCI controller model %d"), model);
> +                       _("PCI controller model was not set correctly"));

You're going to need to precede the string with "%s", to avoid a 
syntax-check failure.


ACK with that fixed.

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

Re: [libvirt] [PATCH 3/6] conf: Make switch statements more strict

Posted by Andrea Bolognani 4 years, 3 months ago
On Wed, 2017-02-22 at 09:40 -0500, Laine Stump wrote:
[...]
> >           virReportError(VIR_ERR_INTERNAL_ERROR,
> > -                       _("Invalid PCI controller model %d"), model);
> > +                       _("PCI controller model was not set correctly"));
> 
> You're going to need to precede the string with "%s", to avoid a 
> syntax-check failure.

I somehow forgot to run the usual "check && syntax-check"
loop before submitting the series, thanks for spotting this!

-- 
Andrea Bolognani / Red Hat / Virtualization

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