diff options
author | Theo Buehler <tb@cvs.openbsd.org> | 2021-03-26 16:28:16 +0000 |
---|---|---|
committer | Theo Buehler <tb@cvs.openbsd.org> | 2021-03-26 16:28:16 +0000 |
commit | 59b21226f5c5ddafb81acd281723a390fad53c42 (patch) | |
tree | 7914ee8d2412a5584ac2ca5aa20966c6809c4792 /usr.sbin/vmctl | |
parent | 32140f68376c2e16a3ce40bf9d5e0edc37cb1a6f (diff) |
Simplify argument parsing of vmctl stop
The previous argument parsing logic had at least three bugs:
a copy-paste error led to an off-by-one and a printf "%s" NULL,
as reported by Preben Guldberg. A previous commit led to a dead
else branch and a use of uninitialized. This can all be avoided
by reworking the logic so as to be readable.
Prompted by a diff from Preben
ok dv
Diffstat (limited to 'usr.sbin/vmctl')
-rw-r--r-- | usr.sbin/vmctl/main.c | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/usr.sbin/vmctl/main.c b/usr.sbin/vmctl/main.c index 249eaa3ded1..6eac8eec8b8 100644 --- a/usr.sbin/vmctl/main.c +++ b/usr.sbin/vmctl/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.62 2020/01/03 05:32:00 pd Exp $ */ +/* $OpenBSD: main.c,v 1.63 2021/03/26 16:28:15 tb Exp $ */ /* * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> @@ -927,7 +927,7 @@ ctl_start(struct parse_result *res, int argc, char *argv[]) int ctl_stop(struct parse_result *res, int argc, char *argv[]) { - int ch, ret; + int ch; while ((ch = getopt(argc, argv, "afw")) != -1) { switch (ch) { @@ -948,20 +948,15 @@ ctl_stop(struct parse_result *res, int argc, char *argv[]) argc -= optind; argv += optind; - if (argc == 0) { - if (res->action != CMD_STOPALL) + if (res->action == CMD_STOPALL) { + if (argc != 0) ctl_usage(res->ctl); - } else if (argc > 1) - ctl_usage(res->ctl); - else if (argc == 1) - ret = parse_vmid(res, argv[0], 0); - else - ret = -1; - - /* VM id is only expected without the -a flag */ - if ((res->action != CMD_STOPALL && ret == -1) || - (res->action == CMD_STOPALL && ret != -1)) - errx(1, "invalid id: %s", argv[1]); + } else { + if (argc != 1) + ctl_usage(res->ctl); + if (parse_vmid(res, argv[0], 0) == -1) + errx(1, "invalid id: %s", argv[0]); + } return (vmmaction(res)); } |