Date: Thu, 25 Dec 2008 10:28:21 GMT From: Hans Petter Selasky <hselasky@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 155270 for review Message-ID: <200812251028.mBPASLee097438@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=155270 Change 155270 by hselasky@hselasky_laptop001 on 2008/12/25 10:27:42 Improve readability of "libusb20_desc_foreach()". Affected files ... .. //depot/projects/usb/src/lib/libusb20/libusb20_desc.c#5 edit Differences ... ==== //depot/projects/usb/src/lib/libusb20/libusb20_desc.c#5 (text+ko) ==== @@ -238,23 +238,37 @@ libusb20_desc_foreach(const struct libusb20_me_struct *pdesc, const uint8_t *psubdesc) { - const void *end; + const uint8_t *start; + const uint8_t *end; + const uint8_t *desc_next; - if (pdesc == NULL) { + /* be NULL safe */ + if (pdesc == NULL) return (NULL); - } - end = LIBUSB20_ADD_BYTES(pdesc->ptr, pdesc->len); + + start = (const uint8_t *)pdesc->ptr; + end = LIBUSB20_ADD_BYTES(start, pdesc->len); + + /* get start of next descriptor */ + if (psubdesc == NULL) + psubdesc = start; + else + psubdesc = psubdesc + psubdesc[0]; + + /* check that the next USB descriptor is within the range */ + if ((psubdesc < start) || (psubdesc >= end)) + return (NULL); /* out of range, or EOD */ + + /* check start of the second next USB descriptor, if any */ + desc_next = psubdesc + psubdesc[0]; + if ((desc_next < start) || (desc_next > end)) + return (NULL); /* out of range */ + + /* check minimum descriptor length */ + if (psubdesc[0] < 3) + return (NULL); /* too short descriptor */ - if (psubdesc == NULL) { - psubdesc = LIBUSB20_ADD_BYTES(pdesc->ptr, 0); - } else { - psubdesc = LIBUSB20_ADD_BYTES(psubdesc, psubdesc[0]); - } - return (((((const void *)psubdesc) >= ((void *)(pdesc->ptr))) && - (((const void *)psubdesc) < end) && - (LIBUSB20_ADD_BYTES(psubdesc, psubdesc[0]) >= ((void *)(pdesc->ptr))) && - (LIBUSB20_ADD_BYTES(psubdesc, psubdesc[0]) <= end) && - (psubdesc[0] >= 3)) ? psubdesc : NULL); + return (psubdesc); /* return start of next descriptor */ } /*------------------------------------------------------------------------*
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200812251028.mBPASLee097438>