Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb Pass the lengths of the buffers and use bound st...



details:   https://anonhg.NetBSD.org/src/rev/030bf15a0199
branches:  trunk
changeset: 756925:030bf15a0199
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Aug 07 21:09:48 2010 +0000

description:
Pass the lengths of the buffers and use bound string functions where easy
to do so.

diffstat:

 sys/dev/usb/usb_subr.c    |  62 +++++++++++++++++++++++++---------------------
 sys/dev/usb/usb_verbose.c |  21 ++++++++-------
 sys/dev/usb/usb_verbose.h |   7 +++--
 3 files changed, 48 insertions(+), 42 deletions(-)

diffs (251 lines):

diff -r 20a75ac0e76d -r 030bf15a0199 sys/dev/usb/usb_subr.c
--- a/sys/dev/usb/usb_subr.c    Sat Aug 07 21:03:18 2010 +0000
+++ b/sys/dev/usb/usb_subr.c    Sat Aug 07 21:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usb_subr.c,v 1.174 2010/07/27 16:15:30 drochner Exp $  */
+/*     $NetBSD: usb_subr.c,v 1.175 2010/08/07 21:09:48 christos Exp $  */
 /*     $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $   */
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.174 2010/07/27 16:15:30 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.175 2010/08/07 21:09:48 christos Exp $");
 
 #include "opt_compat_netbsd.h"
 #include "opt_usbverbose.h"
@@ -70,16 +70,16 @@
 
 Static usbd_status usbd_set_config(usbd_device_handle, int);
 Static void usbd_devinfo(usbd_device_handle, int, char *, size_t);
-Static void usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p,
-                           int usedev, int useencoded);
-Static int usbd_getnewaddr(usbd_bus_handle bus);
+Static void usbd_devinfo_vp(usbd_device_handle, char *, size_t, char *, size_t,
+    int, int);
+Static int usbd_getnewaddr(usbd_bus_handle);
 Static int usbd_print(void *, const char *);
 Static int usbd_ifprint(void *, const char *);
-Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
+Static void usbd_free_iface_data(usbd_device_handle, int);
 Static void usbd_kill_pipe(usbd_pipe_handle);
 usbd_status usbd_attach_roothub(device_t, usbd_device_handle);
-Static usbd_status usbd_probe_and_attach(device_t parent,
-                                 usbd_device_handle dev, int port, int addr);
+Static usbd_status usbd_probe_and_attach(device_t, usbd_device_handle, int,
+    int);
 
 Static u_int32_t usb_cookie_no = 0;
 
@@ -108,11 +108,11 @@
 
 void usb_load_verbose(void);
 
-void get_usb_vendor_stub(char *, usb_vendor_id_t);
-void get_usb_product_stub(char *, usb_vendor_id_t, usb_product_id_t);
+void get_usb_vendor_stub(char *, size_t, usb_vendor_id_t);
+void get_usb_product_stub(char *, size_t, usb_vendor_id_t, usb_product_id_t);
 
-void (*get_usb_vendor)(char *, usb_vendor_id_t) = get_usb_vendor_stub;
-void (*get_usb_product)(char *, usb_vendor_id_t, usb_product_id_t) =
+void (*get_usb_vendor)(char *, size_t, usb_vendor_id_t) = get_usb_vendor_stub;
+void (*get_usb_product)(char *, size_t, usb_vendor_id_t, usb_product_id_t) =
        get_usb_product_stub;
 
 int usb_verbose_loaded = 0;
@@ -129,18 +129,19 @@
        }
 }
 
-void get_usb_vendor_stub(char *v, usb_vendor_id_t v_id)
+void get_usb_vendor_stub(char *v, size_t l, usb_vendor_id_t v_id)
 {
        usb_load_verbose();
        if (usb_verbose_loaded)
-               get_usb_vendor(v, v_id);
+               get_usb_vendor(v, l, v_id);
 }
 
-void get_usb_product_stub(char *p, usb_vendor_id_t v_id, usb_product_id_t p_id)
+void get_usb_product_stub(char *p, size_t l, usb_vendor_id_t v_id,
+    usb_product_id_t p_id)
 {
        usb_load_verbose();
        if (usb_verbose_loaded)
-               get_usb_product(p, v_id, p_id);
+               get_usb_product(p, l, v_id, p_id);
 }
 
 const char *
@@ -207,14 +208,14 @@
 }
 
 Static void
-usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev,
-               int useencoded)
+usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t vl, char *p,
+    size_t pl, int usedev, int useencoded)
 {
        usb_device_descriptor_t *udd = &dev->ddesc;
+       if (dev == NULL)
+               return;
 
        v[0] = p[0] = '\0';
-       if (dev == NULL)
-               return;
 
        if (usedev) {
                if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
@@ -225,21 +226,21 @@
                        usbd_trim_spaces(p);
        }
        if (v[0] == '\0')
-               get_usb_vendor(v, UGETW(udd->idVendor));
+               get_usb_vendor(v, vl, UGETW(udd->idVendor));
        if (p[0] == '\0')
-               get_usb_product(p, UGETW(udd->idVendor), UGETW(udd->idProduct));
+               get_usb_product(p, pl, UGETW(udd->idVendor),
+                   UGETW(udd->idProduct));
 
-       /* There is no need for snprintf below. */
        if (v[0] == '\0')
-               sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
+               snprintf(v, vl, "vendor 0x%04x", UGETW(udd->idVendor));
        if (p[0] == '\0')
-               sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
+               snprintf(p, pl, "product 0x%04x", UGETW(udd->idProduct));
 }
 
 int
 usbd_printBCD(char *cp, size_t l, int bcd)
 {
-       return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
+       return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff);
 }
 
 Static void
@@ -259,7 +260,8 @@
 
        ep = cp + l;
 
-       usbd_devinfo_vp(dev, vendor, product, 1, 1);
+       usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN,
+           product, USB_MAX_ENCODED_STRING_LEN, 1, 1);
        cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
        if (showclass)
                cp += snprintf(cp, ep - cp, ", class %d/%d",
@@ -1353,7 +1355,8 @@
        di->udi_bus = device_unit(dev->bus->usbctl);
        di->udi_addr = dev->address;
        di->udi_cookie = dev->cookie;
-       usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
+       usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
+           di->udi_product, sizeof(di->udi_product), usedev, 1);
        usbd_printBCD(di->udi_release, sizeof(di->udi_release),
            UGETW(dev->ddesc.bcdDevice));
        di->udi_serial[0] = 0;
@@ -1423,7 +1426,8 @@
        di->udi_bus = device_unit(dev->bus->usbctl);
        di->udi_addr = dev->address;
        di->udi_cookie = dev->cookie;
-       usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
+       usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
+           di->udi_product, sizeof(di->udi_product), usedev, 0);
        usbd_printBCD(di->udi_release, sizeof(di->udi_release),
            UGETW(dev->ddesc.bcdDevice));
        di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
diff -r 20a75ac0e76d -r 030bf15a0199 sys/dev/usb/usb_verbose.c
--- a/sys/dev/usb/usb_verbose.c Sat Aug 07 21:03:18 2010 +0000
+++ b/sys/dev/usb/usb_verbose.c Sat Aug 07 21:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usb_verbose.c,v 1.3 2010/07/25 13:19:27 pgoyette Exp $ */
+/*     $NetBSD: usb_verbose.c,v 1.4 2010/08/07 21:09:48 christos Exp $ */
 /*     $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $   */
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb_verbose.c,v 1.3 2010/07/25 13:19:27 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_verbose.c,v 1.4 2010/08/07 21:09:48 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/module.h>
@@ -56,16 +56,16 @@
 
 #include <dev/usb/usbdevs_data.h>
 
-void get_usb_vendor_real(char *, usb_vendor_id_t);
-void get_usb_product_real(char *, usb_vendor_id_t, usb_product_id_t);
+void get_usb_vendor_real(char *, size_t, usb_vendor_id_t);
+void get_usb_product_real(char *, size_t, usb_vendor_id_t, usb_product_id_t);
 
 MODULE(MODULE_CLASS_MISC, usbverbose, NULL);
   
 static int
 usbverbose_modcmd(modcmd_t cmd, void *arg)
 {
-       static void (*saved_usb_vendor)(char *, usb_vendor_id_t);
-       static void (*saved_usb_product)(char *, usb_vendor_id_t,
+       static void (*saved_usb_vendor)(char *, size_t, usb_vendor_id_t);
+       static void (*saved_usb_product)(char *, size_t, usb_vendor_id_t,
                usb_product_id_t);
 
        switch (cmd) {
@@ -86,19 +86,20 @@
        }
 }
 
-void get_usb_vendor_real(char *v, usb_vendor_id_t v_id)
+void get_usb_vendor_real(char *v, size_t vl, usb_vendor_id_t v_id)
 {
        int n;
 
        /* There is no need for strlcpy below. */
        for (n = 0; n < usb_nvendors; n++)
                if (usb_vendors[n].vendor == v_id) {
-                       strcpy(v, usb_vendors[n].vendorname);
+                       strlcpy(v, usb_vendors[n].vendorname, vl);
                        break;
                }
 }
 
-void get_usb_product_real(char *p, usb_vendor_id_t v_id, usb_product_id_t p_id)
+void get_usb_product_real(char *p, size_t pl, usb_vendor_id_t v_id,
+    usb_product_id_t p_id)
 {
        int n;
 
@@ -106,7 +107,7 @@
        for (n = 0; n < usb_nproducts; n++)
                if (usb_products[n].vendor == v_id &&
                    usb_products[n].product == p_id) {
-                       strcpy(p, usb_products[n].productname);
+                       strlcpy(p, usb_products[n].productname, pl);
                        break;
                }
 }
diff -r 20a75ac0e76d -r 030bf15a0199 sys/dev/usb/usb_verbose.h
--- a/sys/dev/usb/usb_verbose.h Sat Aug 07 21:03:18 2010 +0000
+++ b/sys/dev/usb/usb_verbose.h Sat Aug 07 21:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: usb_verbose.h,v 1.2 2010/06/06 18:58:26 pgoyette Exp $ */
+/*      $NetBSD: usb_verbose.h,v 1.3 2010/08/07 21:09:48 christos Exp $ */
 
 /*
  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
@@ -36,8 +36,9 @@
 typedef u_int16_t usb_vendor_id_t;
 typedef u_int16_t usb_product_id_t;
 
-extern void (*get_usb_vendor)(char *, usb_vendor_id_t);
-extern void (*get_usb_product)(char *, usb_vendor_id_t, usb_product_id_t);
+extern void (*get_usb_vendor)(char *, size_t, usb_vendor_id_t);
+extern void (*get_usb_product)(char *, size_t, usb_vendor_id_t,
+    usb_product_id_t);
 
 extern int usb_verbose_loaded;
 



Home | Main Index | Thread Index | Old Index