[libvirt] [PATCH v2 37/42] nwfilter: add default: case to all switch statements

Daniel P. Berrangé berrange at redhat.com
Thu Feb 15 16:43:42 UTC 2018


Even if the compiler has validated that all enum constants have case
statements in a switch, it is not safe to omit a default: case
statement. When assigning a value to a variable / struct field that is
defined with an enum type, nothing prevents an invalid value being
assigned. So defensive code must assume existance of invalid values and
thus all switches should have a default: case.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/nwfilter/nwfilter_ebiptables_driver.c |  2 ++
 src/nwfilter/nwfilter_gentech_driver.c    | 23 ++++++++++++++++++++++-
 src/nwfilter/nwfilter_learnipaddr.c       | 13 ++++++++++---
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/src/nwfilter/nwfilter_ebiptables_driver.c b/src/nwfilter/nwfilter_ebiptables_driver.c
index c624337f4d..3d49884e2e 100644
--- a/src/nwfilter/nwfilter_ebiptables_driver.c
+++ b/src/nwfilter/nwfilter_ebiptables_driver.c
@@ -1129,6 +1129,8 @@ iptablesEnforceDirection(virFirewallPtr fw,
         break;
     case CTDIR_STATUS_OLD:
         break;
+    default:
+        break;
     }
 
     if (rule->tt != VIR_NWFILTER_RULE_DIRECTION_INOUT)
diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c
index 840d419bb4..25822d5185 100644
--- a/src/nwfilter/nwfilter_gentech_driver.c
+++ b/src/nwfilter/nwfilter_gentech_driver.c
@@ -405,6 +405,10 @@ virNWFilterIncludeDefToRuleInst(virNWFilterDriverStatePtr driver,
         break;
     case INSTANTIATE_ALWAYS:
         break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected new filter action %d"), useNewFilter);
+        goto cleanup;
     }
 
     if (VIR_APPEND_ELEMENT(inst->filters,
@@ -555,6 +559,11 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
                 break;
             case INSTANTIATE_ALWAYS:
                 break;
+            default:
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("Unexpected new filter action %d"), useNewFilter);
+                rc = -1;
+                goto error;
             }
 
             rc = virNWFilterDetermineMissingVarsRec(next_filter,
@@ -562,7 +571,7 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
                                                     missing_vars,
                                                     useNewFilter,
                                                     driver);
-
+        error:
             virNWFilterHashTableFree(tmpvars);
 
             virNWFilterObjUnlock(obj);
@@ -701,6 +710,10 @@ virNWFilterDoInstantiate(const unsigned char *vmuuid,
     case INSTANTIATE_ALWAYS:
         instantiate = true;
         break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected new filter action %d"), useNewFilter);
+        goto err_exit;
     }
 
     if (instantiate) {
@@ -825,6 +838,10 @@ virNWFilterInstantiateFilterUpdate(virNWFilterDriverStatePtr driver,
 
     case INSTANTIATE_ALWAYS:
         break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unexpected new filter action %d"), useNewFilter);
+        goto err_exit_vars1;
     }
 
     rc = virNWFilterDoInstantiate(vmuuid, techdriver, filter,
@@ -1102,6 +1119,10 @@ virNWFilterDomainFWUpdateCB(virDomainObjPtr obj,
                                        _("Failure while applying current filter on "
                                          "VM %s"), vm->name);
                     break;
+                default:
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("Unexpected filter step %d"), cb->step);
+                    ret = -1;
                 }
                 if (ret)
                     break;
diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c
index 9ca0639576..e2a3704289 100644
--- a/src/nwfilter/nwfilter_learnipaddr.c
+++ b/src/nwfilter/nwfilter_learnipaddr.c
@@ -351,9 +351,14 @@ procDHCPOpts(struct dhcp *dhcp, int dhcp_opts_len,
                 case DHCP_MSGT_DHCPOFFER:
                     *vmaddr = dhcp->yiaddr;
                     *howDetected = DETECT_DHCP;
-                break;
+                    break;
+                default:
+                    break;
                 }
             }
+            break;
+        default:
+            break;
         }
         dhcp_opts_len -= (2 + dhcpopt->len);
         dhcpopt = (struct dhcp_option*)((char *)dhcpopt + 2 + dhcpopt->len);
@@ -542,11 +547,13 @@ learnIPAddressThread(void *arg)
                     case ARPOP_REPLY:
                         vmaddr = arphdr->ar_sip;
                         howDetected = DETECT_STATIC;
-                    break;
+                        break;
                     case ARPOP_REQUEST:
                         vmaddr = arphdr->ar_tip;
                         howDetected = DETECT_STATIC;
-                    break;
+                        break;
+                    default:
+                        break;
                     }
                 }
             } else if (virMacAddrCmpRaw(&req->macaddr,
-- 
2.14.3




More information about the libvir-list mailing list