Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/cgdconfig Check arg count in configure() a...



details:   https://anonhg.NetBSD.org/src/rev/e25707c35a8d
branches:  trunk
changeset: 318917:e25707c35a8d
user:      kre <kre%NetBSD.org@localhost>
date:      Wed May 09 13:19:33 2018 +0000
description:
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.

diffstat:

 sbin/cgdconfig/cgdconfig.c |  33 ++++++++++++++++-----------------
 1 files changed, 16 insertions(+), 17 deletions(-)

diffs (69 lines):

diff -r f66be20c12ce -r e25707c35a8d sbin/cgdconfig/cgdconfig.c
--- a/sbin/cgdconfig/cgdconfig.c        Wed May 09 13:19:27 2018 +0000
+++ b/sbin/cgdconfig/cgdconfig.c        Wed May 09 13:19:33 2018 +0000
@@ -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 @@
        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 @@
                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 @@
                /* 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;



Home | Main Index | Thread Index | Old Index