Skip to contentSkip to conversation

vl: Abort if multiple machines are registered as default

PATCH In review Philippe Mathieu-Daudé 2 participants 2 messages Feb 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vl: Abort if multiple machines are registered as default

It would be confusing to have multiple default machines. Abort if this ever occurs.

---
v2: Use assert() instead of human friendly message (Marc-André)

Cc Marc-André Lureau <[email protected]>
Cc Laurent Vivier <[email protected]>
---
 vl.c | 4 ++++
 1 file changed, 4 insertions(+)
vl.c +4 -0
index 7dcb0879c4..b3e0f7af25 100644
@@ -2354,6 +2354,8 @@ static MachineClass *machine_parse(const char *name, GSList *machines)
     GSList *el;
 
     if (is_help_option(name)) {
+        int default_count = 0;
+
         printf("Supported machines are:\n");
         machines = g_slist_sort(machines, machine_class_cmp);
         for (el = machines; el; el = el->next) {
@@ -2364,7 +2366,9 @@ static MachineClass *machine_parse(const char *name, GSList *machines)
             printf("%-20s %s%s%s\n", mc->name, mc->desc,
                    mc->is_default ? " (default)" : "",
                    mc->deprecation_reason ? " (deprecated)" : "");
+            default_count += !!mc->is_default;
         }
+        assert(default_count <= 1);
         exit(0);
     }
 
Signature · 1 line
2.21.1
#1 lore raw
Quoted 1 line · Philippe Mathieu-Daudé

+ default_count += !!mc->is_default;

I'd prefer we simply change mc->is_default to bool. There's no good reason for it to be type int, afaics.

In the meantime, there are no settings of is_default outside {0,1}, so you could just drop the !! for now.

r~