Pass the lengths of the buffers and use bound string functions where easy

to do so.
This commit is contained in:
christos 2010-08-07 21:09:48 +00:00
parent 981352168f
commit 2268c39319
3 changed files with 48 additions and 42 deletions

View File

@ -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 @@ extern int usbdebug;
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 @@ Static const char * const usbd_error_strs[] = {
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 usb_load_verbose(void)
}
}
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,15 +208,15 @@ usbd_trim_spaces(char *p)
}
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;
v[0] = p[0] = '\0';
if (dev == NULL)
return;
v[0] = p[0] = '\0';
if (usedev) {
if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
USBD_NORMAL_COMPLETION)
@ -225,21 +226,21 @@ usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev,
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 @@ usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
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 @@ usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
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 @@ usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
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);

View File

@ -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 @@ struct usb_product {
#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 @@ usbverbose_modcmd(modcmd_t cmd, void *arg)
}
}
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 @@ void get_usb_product_real(char *p, usb_vendor_id_t v_id, usb_product_id_t p_id)
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;
}
}

View File

@ -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;