tech-userlevel archive

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

Re: [patch] usbdevs(8) use strtol(3) instead of atoi(3) for more predictable result



Hello,

On 11-24-19, Terry Moore wrote:
> 
> Zero is never a legal address for an operating USB device. (It can appear transiently during enumeration, but there's no way to operate a device in that state; so it make no sense for address zero to be used in usermode). A device at address zero is always under the administration of the hub driver and is about to be moved to a non-zero address.
>

Yes, but usbdevs(8) everywhere counts devices from zero, so probably this should be updated,
if it is ok I will take a look there and fix this if need in a separate patch.

Here is new patch with updates suggested by Kamil to check the given addr with strtoi(3):

Index: usbdevs.c
===================================================================
RCS file: /cvsroot/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.39
diff -u -p -r1.39 usbdevs.c
--- usbdevs.c	12 Nov 2019 07:41:50 -0000	1.39
+++ usbdevs.c	24 Nov 2019 18:53:15 -0000
@@ -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,16 +368,19 @@ 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;
 	int ncont;
+	char *ep;
 
 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
 		switch(ch) {
 		case 'a':
-			addr = atoi(optarg);
+			addr = strtoi(optarg, &ep, 10, 0, USB_MAX_DEVICES - 1, &error);
+			if (error)
+				errx(error, "bad value for device address - %s", optarg);
 			break;
 		case 'd':
 			showdevs++;


Home | Main Index | Thread Index | Old Index