summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortb <tb@openbsd.org>2021-03-26 16:28:15 +0000
committertb <tb@openbsd.org>2021-03-26 16:28:15 +0000
commita261be2ae4cd69815447f58358375c8fab7d1d90 (patch)
tree52bb9dab7fa36167058bc7bdf7968cfa5c3a00e4
parentCompare filepath with strcmp() and not strcasecmp(). The URI in RPKI are (diff)
downloadwireguard-openbsd-a261be2ae4cd69815447f58358375c8fab7d1d90.tar.xz
wireguard-openbsd-a261be2ae4cd69815447f58358375c8fab7d1d90.zip
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
-rw-r--r--usr.sbin/vmctl/main.c25
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));
}