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(a)redhat.com>
---
examples/object-events/event-test.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c
index 8499e0b38e..edb17af82e 100644
--- a/examples/object-events/event-test.c
+++ b/examples/object-events/event-test.c
@@ -49,6 +49,7 @@ connectClose(virConnectPtr conn ATTRIBUTE_UNUSED,
return;
case VIR_CONNECT_CLOSE_REASON_LAST:
+ default:
break;
};
@@ -88,6 +89,7 @@ eventToString(int event)
return "Crashed";
case VIR_DOMAIN_EVENT_LAST:
+ default:
break;
}
@@ -115,6 +117,7 @@ eventDetailToString(int event,
return "Snapshot";
case VIR_DOMAIN_EVENT_DEFINED_LAST:
+ default:
break;
}
break;
@@ -128,6 +131,7 @@ eventDetailToString(int event,
return "Renamed";
case VIR_DOMAIN_EVENT_UNDEFINED_LAST:
+ default:
break;
}
break;
@@ -150,6 +154,7 @@ eventDetailToString(int event,
return "Event wakeup";
case VIR_DOMAIN_EVENT_STARTED_LAST:
+ default:
break;
}
break;
@@ -184,6 +189,7 @@ eventDetailToString(int event,
return "Post-copy Error";
case VIR_DOMAIN_EVENT_SUSPENDED_LAST:
+ default:
break;
}
break;
@@ -203,6 +209,7 @@ eventDetailToString(int event,
return "Post-copy";
case VIR_DOMAIN_EVENT_RESUMED_LAST:
+ default:
break;
}
break;
@@ -231,6 +238,7 @@ eventDetailToString(int event,
return "Snapshot";
case VIR_DOMAIN_EVENT_STOPPED_LAST:
+ default:
break;
}
break;
@@ -247,6 +255,7 @@ eventDetailToString(int event,
return "Host request";
case VIR_DOMAIN_EVENT_SHUTDOWN_LAST:
+ default:
break;
}
break;
@@ -260,6 +269,7 @@ eventDetailToString(int event,
return "Disk";
case VIR_DOMAIN_EVENT_PMSUSPENDED_LAST:
+ default:
break;
}
break;
@@ -270,11 +280,13 @@ eventDetailToString(int event,
return "Panicked";
case VIR_DOMAIN_EVENT_CRASHED_LAST:
+ default:
break;
}
break;
case VIR_DOMAIN_EVENT_LAST:
+ default:
break;
}
@@ -299,6 +311,7 @@ networkEventToString(int event)
return "Stopped";
case VIR_NETWORK_EVENT_LAST:
+ default:
break;
}
@@ -317,6 +330,7 @@ guestAgentLifecycleEventStateToString(int event)
return "Connected";
case VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_STATE_LAST:
+ default:
break;
}
@@ -338,6 +352,7 @@ guestAgentLifecycleEventReasonToString(int event)
return "Channel event";
case VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_LAST:
+ default:
break;
}
@@ -361,6 +376,7 @@ storagePoolEventToString(int event)
case VIR_STORAGE_POOL_EVENT_DELETED:
return "Deleted";
case VIR_STORAGE_POOL_EVENT_LAST:
+ default:
break;
}
return "unknown";
@@ -375,6 +391,7 @@ nodeDeviceEventToString(int event)
case VIR_NODE_DEVICE_EVENT_DELETED:
return "Deleted";
case VIR_NODE_DEVICE_EVENT_LAST:
+ default:
break;
}
return "unknown";
@@ -392,6 +409,7 @@ secretEventToString(int event)
return "Undefined";
case VIR_SECRET_EVENT_LAST:
+ default:
break;
}
@@ -527,6 +545,7 @@ graphicsPhaseToStr(int phase)
return "disconnected";
case VIR_DOMAIN_EVENT_GRAPHICS_LAST:
+ default:
break;
}
@@ -589,6 +608,7 @@ diskChangeReasonToStr(int reason)
return "disk dropped due to startupPolicy";
case VIR_DOMAIN_EVENT_DISK_CHANGE_LAST:
+ default:
break;
}
@@ -623,6 +643,7 @@ trayChangeReasonToStr(int reason)
return "close";
case VIR_DOMAIN_EVENT_TRAY_CHANGE_LAST:
+ default:
break;
}
@@ -874,6 +895,7 @@ blockJobTypeToStr(int type)
switch ((virDomainBlockJobType) type) {
case VIR_DOMAIN_BLOCK_JOB_TYPE_UNKNOWN:
case VIR_DOMAIN_BLOCK_JOB_TYPE_LAST:
+ default:
break;
case VIR_DOMAIN_BLOCK_JOB_TYPE_PULL:
@@ -910,6 +932,7 @@ blockJobStatusToStr(int status)
return "ready";
case VIR_DOMAIN_BLOCK_JOB_LAST:
+ default:
break;
}
@@ -1006,6 +1029,7 @@ metadataTypeToStr(int status)
return "element";
case VIR_DOMAIN_METADATA_LAST:
+ default:
break;
}
--
2.14.3