This changes uiomovei calls to uiomove in usb. It fixes
a few integer truncations due to use of min, and uses
unsigned types for count variables where it makes sense.
This also allows us to get rid of a couple of 'if (len < 0)' checks
that just cannot happen.

udbd_get_cdesc() returns a length value by a pointer. This diff changes
the base type to u_int as well, as all callers pass in unsigned variables
now.

ok?

Index: dev/usb//ugen.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/ugen.c,v
retrieving revision 1.91
diff -u -p -r1.91 ugen.c
--- dev/usb//ugen.c     19 Oct 2015 14:05:01 -0000      1.91
+++ dev/usb//ugen.c     28 Feb 2016 19:10:07 -0000
@@ -468,7 +468,8 @@ int
 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
 {
        struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
-       u_int32_t n, tn;
+       u_int32_t tn;
+       size_t n;
        char buf[UGEN_BBSIZE];
        struct usbd_xfer *xfer;
        usbd_status err;
@@ -523,16 +524,16 @@ ugen_do_read(struct ugen_softc *sc, int 
 
                /* Transfer as many chunks as possible. */
                while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
-                       n = min(sce->q.c_cc, uio->uio_resid);
+                       n = ulmin(sce->q.c_cc, uio->uio_resid);
                        if (n > sizeof(buffer))
                                n = sizeof(buffer);
 
                        /* Remove a small chunk from the input queue. */
                        q_to_b(&sce->q, buffer, n);
-                       DPRINTFN(5, ("ugenread: got %d chars\n", n));
+                       DPRINTFN(5, ("ugenread: got %zu chars\n", n));
 
                        /* Copy the data to the user process. */
-                       error = uiomovei(buffer, n, uio);
+                       error = uiomove(buffer, n, uio);
                        if (error)
                                break;
                }
@@ -546,8 +547,8 @@ ugen_do_read(struct ugen_softc *sc, int 
                        flags |= USBD_SHORT_XFER_OK;
                if (sce->timeout == 0)
                        flags |= USBD_CATCH;
-               while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
-                       DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
+               while ((n = ulmin(UGEN_BBSIZE, uio->uio_resid)) != 0) {
+                       DPRINTFN(1, ("ugenread: start transfer %zu bytes\n",n));
                        usbd_setup_xfer(xfer, sce->pipeh, 0, buf, n,
                            flags, sce->timeout, NULL);
                        err = usbd_transfer(xfer);
@@ -562,8 +563,8 @@ ugen_do_read(struct ugen_softc *sc, int 
                                break;
                        }
                        usbd_get_xfer_status(xfer, NULL, NULL, &tn, NULL);
-                       DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
-                       error = uiomovei(buf, tn, uio);
+                       DPRINTFN(1, ("ugenread: got %u bytes\n", tn));
+                       error = uiomove(buf, tn, uio);
                        if (error || tn < n)
                                break;
                }
@@ -594,14 +595,14 @@ ugen_do_read(struct ugen_softc *sc, int 
 
                while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
                        if(sce->fill > sce->cur)
-                               n = min(sce->fill - sce->cur, uio->uio_resid);
+                               n = ulmin(sce->fill - sce->cur, uio->uio_resid);
                        else
-                               n = min(sce->limit - sce->cur, uio->uio_resid);
+                               n = ulmin(sce->limit - sce->cur, 
uio->uio_resid);
 
-                       DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
+                       DPRINTFN(5, ("ugenread: isoc got %zu chars\n", n));
 
                        /* Copy the data to the user process. */
-                       error = uiomovei(sce->cur, n, uio);
+                       error = uiomove(sce->cur, n, uio);
                        if (error)
                                break;
                        sce->cur += n;
@@ -638,7 +639,7 @@ int
 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
 {
        struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
-       u_int32_t n;
+       size_t n;
        int flags, error = 0;
        char buf[UGEN_BBSIZE];
        struct usbd_xfer *xfer;
@@ -671,11 +672,11 @@ ugen_do_write(struct ugen_softc *sc, int
                xfer = usbd_alloc_xfer(sc->sc_udev);
                if (xfer == 0)
                        return (EIO);
-               while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
-                       error = uiomovei(buf, n, uio);
+               while ((n = ulmin(UGEN_BBSIZE, uio->uio_resid)) != 0) {
+                       error = uiomove(buf, n, uio);
                        if (error)
                                break;
-                       DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
+                       DPRINTFN(1, ("ugenwrite: transfer %zu bytes\n", n));
                        usbd_setup_xfer(xfer, sce->pipeh, 0, buf, n,
                            flags, sce->timeout, NULL);
                        err = usbd_transfer(xfer);
@@ -696,12 +697,12 @@ ugen_do_write(struct ugen_softc *sc, int
                xfer = usbd_alloc_xfer(sc->sc_udev);
                if (xfer == 0)
                        return (EIO);
-               while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
+               while ((n = ulmin(UGETW(sce->edesc->wMaxPacketSize),
                    uio->uio_resid)) != 0) {
-                       error = uiomovei(buf, n, uio);
+                       error = uiomove(buf, n, uio);
                        if (error)
                                break;
-                       DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
+                       DPRINTFN(1, ("ugenwrite: transfer %zu bytes\n", n));
                        usbd_setup_xfer(xfer, sce->pipeh, 0, buf, n,
                            flags, sce->timeout, NULL);
                        err = usbd_transfer(xfer);
@@ -1106,7 +1107,7 @@ ugen_do_ioctl(struct ugen_softc *sc, int
                break;
        case USB_GET_FULL_DESC:
        {
-               int len;
+               u_int len;
                struct iovec iov;
                struct uio uio;
                struct usb_full_desc *fd = (struct usb_full_desc *)addr;
@@ -1126,7 +1127,7 @@ ugen_do_ioctl(struct ugen_softc *sc, int
                uio.uio_segflg = UIO_USERSPACE;
                uio.uio_rw = UIO_READ;
                uio.uio_procp = p;
-               error = uiomovei((void *)cdesc, len, &uio);
+               error = uiomove((void *)cdesc, len, &uio);
                free(cdesc, M_TEMP, 0);
                return (error);
        }
@@ -1143,7 +1144,7 @@ ugen_do_ioctl(struct ugen_softc *sc, int
        case USB_DO_REQUEST:
        {
                struct usb_ctl_request *ur = (void *)addr;
-               int len = UGETW(ur->ucr_request.wLength);
+               size_t len = UGETW(ur->ucr_request.wLength);
                struct iovec iov;
                struct uio uio;
                void *ptr = 0;
@@ -1160,7 +1161,7 @@ ugen_do_ioctl(struct ugen_softc *sc, int
                     ur->ucr_request.bRequest == UR_SET_INTERFACE))
                        return (EINVAL);
 
-               if (len < 0 || len > 32767)
+               if (len > 32767)
                        return (EINVAL);
                if (len != 0) {
                        iov.iov_base = (caddr_t)ur->ucr_data;
@@ -1176,7 +1177,7 @@ ugen_do_ioctl(struct ugen_softc *sc, int
                        uio.uio_procp = p;
                        ptr = malloc(len, M_TEMP, M_WAITOK);
                        if (uio.uio_rw == UIO_WRITE) {
-                               error = uiomovei(ptr, len, &uio);
+                               error = uiomove(ptr, len, &uio);
                                if (error)
                                        goto ret;
                        }
@@ -1193,7 +1194,7 @@ ugen_do_ioctl(struct ugen_softc *sc, int
                        len = ur->ucr_actlen;
                if (len != 0) {
                        if (uio.uio_rw == UIO_READ) {
-                               error = uiomovei(ptr, len, &uio);
+                               error = uiomove(ptr, len, &uio);
                                if (error)
                                        goto ret;
                        }
Index: dev/usb//uhid.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/uhid.c,v
retrieving revision 1.64
diff -u -p -r1.64 uhid.c
--- dev/usb//uhid.c     28 Feb 2016 17:57:50 -0000      1.64
+++ dev/usb//uhid.c     28 Feb 2016 19:10:07 -0000
@@ -281,16 +281,16 @@ uhid_do_read(struct uhid_softc *sc, stru
 
        /* Transfer as many chunks as possible. */
        while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
-               length = min(sc->sc_q.c_cc, uio->uio_resid);
+               length = ulmin(sc->sc_q.c_cc, uio->uio_resid);
                if (length > sizeof(buffer))
                        length = sizeof(buffer);
 
                /* Remove a small chunk from the input queue. */
                (void) q_to_b(&sc->sc_q, buffer, length);
-               DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
+               DPRINTFN(5, ("uhidread: got %zu chars\n", length));
 
                /* Copy the data to the user process. */
-               if ((error = uiomovei(buffer, length, uio)) != 0)
+               if ((error = uiomove(buffer, length, uio)) != 0)
                        break;
        }
 
@@ -327,7 +327,7 @@ uhid_do_write(struct uhid_softc *sc, str
        error = 0;
        if (uio->uio_resid != size)
                return (EINVAL);
-       error = uiomovei(sc->sc_obuf, size, uio);
+       error = uiomove(sc->sc_obuf, size, uio);
        if (!error) {
                if (uhidev_set_report(sc->sc_hdev.sc_parent,
                    UHID_OUTPUT_REPORT, sc->sc_hdev.sc_report_id, sc->sc_obuf,
Index: dev/usb//ulpt.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/ulpt.c,v
retrieving revision 1.52
diff -u -p -r1.52 ulpt.c
--- dev/usb//ulpt.c     11 Dec 2015 16:07:02 -0000      1.52
+++ dev/usb//ulpt.c     28 Feb 2016 19:10:07 -0000
@@ -601,7 +601,7 @@ ulptclose(dev_t dev, int flag, int mode,
 int
 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
 {
-       u_int32_t n;
+       size_t n;
        int error = 0;
        void *bufp;
        struct usbd_xfer *xfer;
@@ -616,12 +616,12 @@ ulpt_do_write(struct ulpt_softc *sc, str
                usbd_free_xfer(xfer);
                return (ENOMEM);
        }
-       while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
+       while ((n = ulmin(ULPT_BSIZE, uio->uio_resid)) != 0) {
                ulpt_statusmsg(ulpt_status(sc), sc);
-               error = uiomovei(bufp, n, uio);
+               error = uiomove(bufp, n, uio);
                if (error)
                        break;
-               DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
+               DPRINTFN(1, ("ulptwrite: transfer %zu bytes\n", n));
                usbd_setup_xfer(xfer, sc->sc_out_pipe, 0, bufp, n,
                    USBD_NO_COPY | USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL);
                err = usbd_transfer(xfer);
Index: dev/usb//usb.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/usb.c,v
retrieving revision 1.109
diff -u -p -r1.109 usb.c
--- dev/usb//usb.c      22 Jan 2016 13:31:47 -0000      1.109
+++ dev/usb//usb.c      28 Feb 2016 19:10:07 -0000
@@ -610,7 +610,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
        case USB_REQUEST:
        {
                struct usb_ctl_request *ur = (void *)data;
-               int len = UGETW(ur->ucr_request.wLength);
+               size_t len = UGETW(ur->ucr_request.wLength);
                struct iovec iov;
                struct uio uio;
                void *ptr = 0;
@@ -621,7 +621,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
                if (!(flag & FWRITE))
                        return (EBADF);
 
-               DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
+               DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%zu\n", addr, len));
                /* Avoid requests that would damage the bus integrity. */
                if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
                     ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
@@ -631,7 +631,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
                     ur->ucr_request.bRequest == UR_SET_INTERFACE))
                        return (EINVAL);
 
-               if (len < 0 || len > 32767)
+               if (len > 32767)
                        return (EINVAL);
                if (addr < 0 || addr >= USB_MAX_DEVICES)
                        return (EINVAL);
@@ -651,7 +651,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
                        uio.uio_procp = p;
                        ptr = malloc(len, M_TEMP, M_WAITOK);
                        if (uio.uio_rw == UIO_WRITE) {
-                               error = uiomovei(ptr, len, &uio);
+                               error = uiomove(ptr, len, &uio);
                                if (error)
                                        goto ret;
                        }
@@ -668,7 +668,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
                        len = ur->ucr_actlen;
                if (len != 0) {
                        if (uio.uio_rw == UIO_READ) {
-                               error = uiomovei(ptr, len, &uio);
+                               error = uiomove(ptr, len, &uio);
                                if (error)
                                        goto ret;
                        }
@@ -766,7 +766,8 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
                usb_config_descriptor_t *cdesc;
                struct iovec iov;
                struct uio uio;
-               int len, error;
+               int error;
+               size_t len;
 
                if (addr < 1 || addr >= USB_MAX_DEVICES)
                        return (EINVAL);
@@ -797,7 +798,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
                uio.uio_segflg = UIO_USERSPACE;
                uio.uio_rw = UIO_READ;
                uio.uio_procp = p;
-               error = uiomovei((void *)cdesc, len, &uio);
+               error = uiomove((void *)cdesc, len, &uio);
                free(cdesc, M_TEMP, 0);
                return (error);
        }
Index: dev/usb//usb_subr.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/usb_subr.c,v
retrieving revision 1.119
diff -u -p -r1.119 usb_subr.c
--- dev/usb//usb_subr.c 14 Jan 2016 19:53:59 -0000      1.119
+++ dev/usb//usb_subr.c 28 Feb 2016 19:10:07 -0000
@@ -1310,10 +1310,10 @@ usbd_fill_deviceinfo(struct usbd_device 
 
 /* Retrieve a complete descriptor for a certain device and index. */
 usb_config_descriptor_t *
-usbd_get_cdesc(struct usbd_device *dev, int index, int *lenp)
+usbd_get_cdesc(struct usbd_device *dev, int index, u_int *lenp)
 {
        usb_config_descriptor_t *cdesc, *tdesc, cdescr;
-       int len;
+       u_int len;
        usbd_status err;
 
        if (index == USB_CURRENT_CONFIG_INDEX) {
@@ -1325,14 +1325,14 @@ usbd_get_cdesc(struct usbd_device *dev, 
                        *lenp = len;
                cdesc = malloc(len, M_TEMP, M_WAITOK);
                memcpy(cdesc, tdesc, len);
-               DPRINTFN(5,("usbd_get_cdesc: current, len=%d\n", len));
+               DPRINTFN(5,("usbd_get_cdesc: current, len=%u\n", len));
        } else {
                err = usbd_get_desc(dev, UDESC_CONFIG, index,
                    USB_CONFIG_DESCRIPTOR_SIZE, &cdescr);
                if (err || cdescr.bDescriptorType != UDESC_CONFIG)
                        return (0);
                len = UGETW(cdescr.wTotalLength);
-               DPRINTFN(5,("usbd_get_cdesc: index=%d, len=%d\n", index, len));
+               DPRINTFN(5,("usbd_get_cdesc: index=%d, len=%u\n", index, len));
                if (lenp)
                        *lenp = len;
                cdesc = malloc(len, M_TEMP, M_WAITOK);
Index: dev/usb//usbdi.h
===================================================================
RCS file: /cvs/src/sys/dev/usb/usbdi.h,v
retrieving revision 1.66
diff -u -p -r1.66 usbdi.h
--- dev/usb//usbdi.h    10 Jul 2015 15:47:48 -0000      1.66
+++ dev/usb//usbdi.h    28 Feb 2016 19:10:07 -0000
@@ -130,7 +130,7 @@ usb_device_descriptor_t *usbd_get_device
 usbd_status usbd_set_interface(struct usbd_interface *, int);
 int usbd_get_no_alts(usb_config_descriptor_t *, int);
 void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *, int);
-usb_config_descriptor_t *usbd_get_cdesc(struct usbd_device *, int, int *);
+usb_config_descriptor_t *usbd_get_cdesc(struct usbd_device *, int, u_int *);
 int usbd_get_interface_altindex(struct usbd_interface *iface);
 
 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,

Reply via email to