Module Name: src
Committed By: kre
Date: Wed May 9 13:19:33 UTC 2018
Modified Files:
src/sbin/cgdconfig: cgdconfig.c
Log Message:
Check arg count in configure() at entry, rather than later.
This avoids the stupid null deref I added a couple of commits
ago (on bad usage) and also simplifies the rest of the routine
which no longer needs to check the arg count nearly as much.
Thanks to Alexander Nasonov for finding the null deref bug.
To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/sbin/cgdconfig/cgdconfig.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sbin/cgdconfig/cgdconfig.c
diff -u src/sbin/cgdconfig/cgdconfig.c:1.43 src/sbin/cgdconfig/cgdconfig.c:1.44
--- src/sbin/cgdconfig/cgdconfig.c:1.43 Sun May 6 20:55:42 2018
+++ src/sbin/cgdconfig/cgdconfig.c Wed May 9 13:19:33 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: cgdconfig.c,v 1.43 2018/05/06 20:55:42 kre Exp $ */
+/* $NetBSD: cgdconfig.c,v 1.44 2018/05/09 13:19:33 kre Exp $ */
/*-
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 2002, 2003\
The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: cgdconfig.c,v 1.43 2018/05/06 20:55:42 kre Exp $");
+__RCSID("$NetBSD: cgdconfig.c,v 1.44 2018/05/09 13:19:33 kre Exp $");
#endif
#include <err.h>
@@ -515,6 +515,15 @@ configure(int argc, char **argv, struct
char devicename[PATH_MAX];
const char *dev = NULL; /* XXX: gcc */
+ if (argc < 2 || argc > 3) {
+ /* print usage and exit, only if called from main() */
+ if (flags == CONFIG_FLAGS_FROMMAIN) {
+ warnx("wrong number of args");
+ usage();
+ }
+ return -1;
+ }
+
if ((
fd = opendisk1(*argv, O_RDWR, cgdname, sizeof(cgdname), 1, prog_open)
) != -1) {
@@ -529,12 +538,10 @@ configure(int argc, char **argv, struct
prog_close(fd);
}
- if (argc == 2 || argc == 3) {
- dev = getfsspecname(devicename, sizeof(devicename), argv[1]);
- if (dev == NULL) {
- warnx("getfsspecname failed: %s", devicename);
- return -1;
- }
+ dev = getfsspecname(devicename, sizeof(devicename), argv[1]);
+ if (dev == NULL) {
+ warnx("getfsspecname failed: %s", devicename);
+ return -1;
}
if (argc == 2) {
@@ -543,16 +550,8 @@ configure(int argc, char **argv, struct
/* make string writable for basename */
strlcpy(pfile, dev, sizeof(pfile));
p = params_cget(basename(pfile));
- } else if (argc == 3) {
+ } else
p = params_cget(argv[2]);
- } else {
- /* print usage and exit, only if called from main() */
- if (flags == CONFIG_FLAGS_FROMMAIN) {
- warnx("wrong number of args");
- usage();
- }
- return -1;
- }
if (!p)
return -1;