PERFORCE change 155270 for review

Hans Petter Selasky hselasky at FreeBSD.org
Thu Dec 25 02:28:22 PST 2008


http://perforce.freebsd.org/chv.cgi?CH=155270

Change 155270 by hselasky at 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 */
 }
 
 /*------------------------------------------------------------------------*


More information about the p4-projects mailing list