Module Name:    src
Committed By:   christos
Date:           Wed Nov 27 17:56:09 UTC 2019

Modified Files:
        src/usr.sbin/usbdevs: usbdevs.c

Log Message:
Use strtoi instead of atoi() to catch bad input (Alexander Kuleshov)


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/usr.sbin/usbdevs/usbdevs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/usbdevs/usbdevs.c
diff -u src/usr.sbin/usbdevs/usbdevs.c:1.39 src/usr.sbin/usbdevs/usbdevs.c:1.40
--- src/usr.sbin/usbdevs/usbdevs.c:1.39	Tue Nov 12 02:41:50 2019
+++ src/usr.sbin/usbdevs/usbdevs.c	Wed Nov 27 12:56:08 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdevs.c,v 1.39 2019/11/12 07:41:50 mrg Exp $	*/
+/*	$NetBSD: usbdevs.c,v 1.40 2019/11/27 17:56:08 christos Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: usbdevs.c,v 1.39 2019/11/12 07:41:50 mrg Exp $");
+__RCSID("$NetBSD: usbdevs.c,v 1.40 2019/11/27 17:56:08 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -48,6 +48,7 @@ __RCSID("$NetBSD: usbdevs.c,v 1.39 2019/
 #include <langinfo.h>
 #include <iconv.h>
 #include <ctype.h>
+#include <inttypes.h>
 
 #include <dev/usb/usb.h>
 
@@ -367,7 +368,7 @@ getusbcount_device(int fd, const char *d
 int
 main(int argc, char **argv)
 {
-	int ch, i, f;
+	int ch, i, f, error;
 	char buf[50];
 	char *dev = NULL;
 	int addr = -1;
@@ -376,7 +377,13 @@ main(int argc, char **argv)
 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
 		switch(ch) {
 		case 'a':
-			addr = atoi(optarg);
+			addr = strtoi(optarg, NULL, 10, 0, USB_MAX_DEVICES - 1,
+			    &error);
+			if (error) {
+				errc(EXIT_FAILURE, error,
+				    "Bad value for device address: `%s'",
+				    optarg);
+			}
 			break;
 		case 'd':
 			showdevs++;
@@ -429,5 +436,5 @@ main(int argc, char **argv)
 		else
 			err(1, "%s", dev);
 	}
-	exit(EXIT_SUCCESS);
+	return EXIT_SUCCESS;
 }

Reply via email to