From owner-p4-projects@FreeBSD.ORG Thu Dec 25 10:28:21 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id B20B81065674; Thu, 25 Dec 2008 10:28:21 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 768C31065673 for ; Thu, 25 Dec 2008 10:28:21 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 672788FC17 for ; Thu, 25 Dec 2008 10:28:21 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id mBPASLfa097440 for ; Thu, 25 Dec 2008 10:28:21 GMT (envelope-from hselasky@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id mBPASLee097438 for perforce@freebsd.org; Thu, 25 Dec 2008 10:28:21 GMT (envelope-from hselasky@FreeBSD.org) Date: Thu, 25 Dec 2008 10:28:21 GMT Message-Id: <200812251028.mBPASLee097438@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky To: Perforce Change Reviews Cc: Subject: PERFORCE change 155270 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Dec 2008 10:28:22 -0000 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 */ } /*------------------------------------------------------------------------*