fuzz_descriptor_parsers.c:clear_configuration:
  337|    518|{
  338|    518|	uint8_t i;
  339|       |
  340|    518|	if (config->interface) {
  ------------------
  |  Branch (340:6): [True: 518, False: 0]
  ------------------
  341|  3.64k|		for (i = 0; i < config->bNumInterfaces; i++)
  ------------------
  |  Branch (341:15): [True: 3.12k, False: 518]
  ------------------
  342|  3.12k|			clear_interface((struct libusb_interface *)
  343|  3.12k|					config->interface + i);
  344|    518|	}
  345|    518|	free((void *)config->interface);
  346|    518|	config->interface = NULL;
  347|    518|	config->bNumInterfaces = 0;
  348|    518|	free((void *)config->extra);
  349|       |	config->extra = NULL;
  350|    518|	config->extra_length = 0;
  351|    518|}
fuzz_descriptor_parsers.c:clear_interface:
  143|  3.33k|{
  144|  3.33k|	int i;
  145|       |
  146|  3.33k|	if (usb_interface->altsetting) {
  ------------------
  |  Branch (146:6): [True: 871, False: 2.45k]
  ------------------
  147|  4.87k|		for (i = 0; i < usb_interface->num_altsetting; i++) {
  ------------------
  |  Branch (147:15): [True: 4.00k, False: 871]
  ------------------
  148|  4.00k|			struct libusb_interface_descriptor *ifp =
  149|  4.00k|				(struct libusb_interface_descriptor *)
  150|  4.00k|				usb_interface->altsetting + i;
  151|       |
  152|  4.00k|			free((void *)ifp->extra);
  153|  4.00k|			ifp->extra = NULL;
  154|  4.00k|			ifp->extra_length = 0;
  155|  4.00k|			if (ifp->endpoint) {
  ------------------
  |  Branch (155:8): [True: 3.15k, False: 855]
  ------------------
  156|  3.15k|				uint8_t j;
  157|       |
  158|  7.29k|				for (j = 0; j < ifp->bNumEndpoints; j++)
  ------------------
  |  Branch (158:17): [True: 4.14k, False: 3.15k]
  ------------------
  159|  4.14k|					clear_endpoint((struct libusb_endpoint_descriptor *)
  160|  4.14k|						       ifp->endpoint + j);
  161|  3.15k|			}
  162|  4.00k|			free((void *)ifp->endpoint);
  163|  4.00k|			ifp->endpoint = NULL;
  164|  4.00k|			ifp->bNumEndpoints = 0;
  165|  4.00k|		}
  166|    871|	}
  167|  3.33k|	free((void *)usb_interface->altsetting);
  168|       |	usb_interface->altsetting = NULL;
  169|  3.33k|	usb_interface->num_altsetting = 0;
  170|  3.33k|}
fuzz_descriptor_parsers.c:clear_endpoint:
   50|  4.14k|{
   51|  4.14k|	free((void *)endpoint->extra);
   52|  4.14k|}
fuzz_descriptor_parsers.c:ReadLittleEndian16:
   36|  2.72k|{
   37|  2.72k|	return (uint16_t)((uint16_t)p[1] << 8 |
   38|  2.72k|			  (uint16_t)p[0]);
   39|  2.72k|}
fuzz_descriptor_parsers.c:parse_configuration:
  355|    607|{
  356|    607|	uint8_t i;
  357|    607|	int r;
  358|    607|	const struct usbi_descriptor_header *header;
  359|    607|	struct libusb_interface *usb_interface;
  360|       |
  361|    607|	if (size < LIBUSB_DT_CONFIG_SIZE) {
  ------------------
  |  |  384|    607|#define LIBUSB_DT_CONFIG_SIZE			9
  ------------------
  |  Branch (361:6): [True: 0, False: 607]
  ------------------
  362|      0|		usbi_err(ctx, "short config descriptor read %d/%d",
  ------------------
  |  |  339|      0|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      0|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  363|      0|			 size, LIBUSB_DT_CONFIG_SIZE);
  364|      0|		return LIBUSB_ERROR_IO;
  365|      0|	}
  366|       |
  367|    607|	uint8_t bLength = buffer[0];
  368|    607|	uint8_t bDescriptorType = buffer[1];
  369|    607|	uint16_t wTotalLength = ReadLittleEndian16(&buffer[2]);
  370|    607|	uint8_t bNumInterfaces = buffer[4];
  371|    607|	uint8_t bConfigurationValue = buffer[5];
  372|    607|	uint8_t iConfiguration = buffer[6];
  373|    607|	uint8_t bmAttributes = buffer[7];
  374|    607|	uint8_t MaxPower = buffer[8];
  375|       |
  376|    607|	if (bDescriptorType != LIBUSB_DT_CONFIG) {
  ------------------
  |  Branch (376:6): [True: 66, False: 541]
  ------------------
  377|     66|		usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  ------------------
  |  |  339|     66|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     66|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  378|     66|			 bDescriptorType, LIBUSB_DT_CONFIG);
  379|     66|		return LIBUSB_ERROR_IO;
  380|    541|	} else if (bLength < LIBUSB_DT_CONFIG_SIZE) {
  ------------------
  |  |  384|    541|#define LIBUSB_DT_CONFIG_SIZE			9
  ------------------
  |  Branch (380:13): [True: 10, False: 531]
  ------------------
  381|     10|		usbi_err(ctx, "invalid config bLength (%u)", bLength);
  ------------------
  |  |  339|     10|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     10|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  382|     10|		return LIBUSB_ERROR_IO;
  383|    531|	} else if (bLength > size) {
  ------------------
  |  Branch (383:13): [True: 8, False: 523]
  ------------------
  384|      8|		usbi_err(ctx, "short config descriptor read %d/%u",
  ------------------
  |  |  339|      8|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      8|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  385|      8|			 size, bLength);
  386|      8|		return LIBUSB_ERROR_IO;
  387|    523|	} else if (bNumInterfaces > USB_MAXINTERFACES) {
  ------------------
  |  |  159|    523|#define USB_MAXINTERFACES	32
  ------------------
  |  Branch (387:13): [True: 5, False: 518]
  ------------------
  388|      5|		usbi_err(ctx, "too many interfaces (%u)", bNumInterfaces);
  ------------------
  |  |  339|      5|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      5|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  389|      5|		return LIBUSB_ERROR_IO;
  390|      5|	}
  391|       |
  392|    518|	usb_interface = (struct libusb_interface *)calloc(bNumInterfaces, sizeof(*usb_interface));
  393|    518|	if (!usb_interface)
  ------------------
  |  Branch (393:6): [True: 0, False: 518]
  ------------------
  394|      0|		return LIBUSB_ERROR_NO_MEM;
  395|       |
  396|    518|	buffer += bLength;
  397|    518|	size -= bLength;
  398|       |
  399|  1.20k|	for (i = 0; i < bNumInterfaces; i++) {
  ------------------
  |  Branch (399:14): [True: 1.16k, False: 42]
  ------------------
  400|  1.16k|		const uint8_t *begin;
  401|       |
  402|       |		/* Skip over the rest of the Class Specific or Vendor */
  403|       |		/*  Specific descriptors */
  404|  1.16k|		begin = buffer;
  405|  2.03k|		while (size >= DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  2.03k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (405:10): [True: 1.93k, False: 102]
  ------------------
  406|  1.93k|			header = (const struct usbi_descriptor_header *)buffer;
  407|  1.93k|			if (header->bLength < DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  1.93k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (407:8): [True: 12, False: 1.92k]
  ------------------
  408|     12|				usbi_err(ctx,
  ------------------
  |  |  339|     12|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     12|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  409|     12|					 "invalid extra config desc len (%u)",
  410|     12|					 header->bLength);
  411|     12|				r = LIBUSB_ERROR_IO;
  412|     12|				goto err;
  413|  1.92k|			} else if (header->bLength > size) {
  ------------------
  |  Branch (413:15): [True: 90, False: 1.83k]
  ------------------
  414|     90|				usbi_warn(ctx,
  ------------------
  |  |  340|     90|#define usbi_warn(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     90|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  415|     90|					  "short extra config desc read %d/%u",
  416|     90|					  size, header->bLength);
  417|     90|				bNumInterfaces = i;
  418|     90|				goto finish;
  419|     90|			}
  420|       |
  421|       |			/* If we find another "proper" descriptor then we're done */
  422|  1.83k|			if (header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
  ------------------
  |  Branch (422:8): [True: 7, False: 1.82k]
  ------------------
  423|  1.82k|			    header->bDescriptorType == LIBUSB_DT_INTERFACE ||
  ------------------
  |  Branch (423:8): [True: 908, False: 917]
  ------------------
  424|    917|			    header->bDescriptorType == LIBUSB_DT_CONFIG ||
  ------------------
  |  Branch (424:8): [True: 31, False: 886]
  ------------------
  425|    886|			    header->bDescriptorType == LIBUSB_DT_DEVICE)
  ------------------
  |  Branch (425:8): [True: 12, False: 874]
  ------------------
  426|    958|				break;
  427|       |
  428|    874|			usbi_dbg(ctx, "skipping descriptor 0x%x", header->bDescriptorType);
  ------------------
  |  |  342|    874|#define usbi_dbg(ctx ,...)      	_usbi_log(ctx, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|    874|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  429|    874|			buffer += header->bLength;
  430|    874|			size -= header->bLength;
  431|    874|		}
  432|       |
  433|       |		/* Copy any unknown descriptors into a storage area for */
  434|       |		/*  drivers to later parse */
  435|  1.06k|		ptrdiff_t len = buffer - begin;
  436|  1.06k|		if (len > 0) {
  ------------------
  |  Branch (436:7): [True: 38, False: 1.02k]
  ------------------
  437|     38|			size_t new_extra_len = (size_t)(config->extra_length) + (size_t)len;
  438|     38|			uint8_t *new_extra = (uint8_t *)realloc((void *)config->extra, new_extra_len);
  439|       |
  440|     38|			if (!new_extra) {
  ------------------
  |  Branch (440:8): [True: 0, False: 38]
  ------------------
  441|      0|				r = LIBUSB_ERROR_NO_MEM;
  442|      0|				goto err;
  443|      0|			}
  444|       |
  445|     38|			memcpy(new_extra + config->extra_length, begin, (size_t)len);
  446|     38|			config->extra = new_extra;
  447|     38|			config->extra_length = (int)new_extra_len;
  448|     38|		}
  449|       |
  450|  1.06k|		r = parse_interface(ctx, usb_interface + i, buffer, size);
  451|  1.06k|		if (r < 0)
  ------------------
  |  Branch (451:7): [True: 201, False: 859]
  ------------------
  452|    201|			goto err;
  453|    859|		if (r == 0) {
  ------------------
  |  Branch (453:7): [True: 173, False: 686]
  ------------------
  454|    173|			bNumInterfaces = i;
  455|    173|			break;
  456|    173|		}
  457|       |
  458|    686|		buffer += r;
  459|    686|		size -= r;
  460|    686|	}
  461|       |
  462|    305|finish:
  463|    305|	config->interface = usb_interface;
  464|    305|	config->bNumInterfaces = bNumInterfaces;
  465|    305|	config->bLength = bLength;
  466|    305|	config->bDescriptorType = bDescriptorType;
  467|    305|	config->wTotalLength = wTotalLength;
  468|    305|	config->bConfigurationValue = bConfigurationValue;
  469|    305|	config->iConfiguration = iConfiguration;
  470|    305|	config->bmAttributes = bmAttributes;
  471|    305|	config->MaxPower = MaxPower;
  472|       |
  473|    305|	return size;
  474|       |
  475|    213|err:
  476|       |	/* Even though we've failed, set these 2 fields so clear_configuration() can dealloc memory. */
  477|    213|	config->interface = usb_interface;
  478|    213|	config->bNumInterfaces = bNumInterfaces;
  479|       |
  480|    213|	clear_configuration(config);
  481|    213|	return r;
  482|    518|}
fuzz_descriptor_parsers.c:parse_interface:
  174|  1.06k|{
  175|  1.06k|	int r;
  176|  1.06k|	int parsed = 0;
  177|  1.06k|	int interface_number = -1;
  178|  1.06k|	const struct usbi_descriptor_header *header;
  179|  1.06k|	const struct usbi_interface_descriptor *if_desc;
  180|  1.06k|	struct libusb_interface_descriptor *ifp;
  181|  1.06k|	const uint8_t *begin;
  182|       |
  183|  4.20k|	while (size >= LIBUSB_DT_INTERFACE_SIZE) {
  ------------------
  |  |  385|  4.20k|#define LIBUSB_DT_INTERFACE_SIZE		9
  ------------------
  |  Branch (183:9): [True: 4.06k, False: 142]
  ------------------
  184|  4.06k|		uint8_t bLength = buffer[0];
  185|  4.06k|		uint8_t bDescriptorType = buffer[1];
  186|  4.06k|		uint8_t bInterfaceNumber = buffer[2];
  187|  4.06k|		uint8_t bAlternateSetting = buffer[3];
  188|  4.06k|		uint8_t bNumEndpoints = buffer[4];
  189|  4.06k|		uint8_t bInterfaceClass = buffer[5];
  190|  4.06k|		uint8_t bInterfaceSubClass = buffer[6];
  191|  4.06k|		uint8_t bInterfaceProtocol = buffer[7];
  192|  4.06k|		uint8_t iInterface = buffer[8];
  193|       |
  194|  4.06k|		if (bDescriptorType != LIBUSB_DT_INTERFACE) {
  ------------------
  |  Branch (194:7): [True: 31, False: 4.03k]
  ------------------
  195|     31|			usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  ------------------
  |  |  339|     31|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     31|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  196|     31|				 bDescriptorType, LIBUSB_DT_INTERFACE);
  197|     31|			return parsed;
  198|  4.03k|		} else if (bLength < LIBUSB_DT_INTERFACE_SIZE) {
  ------------------
  |  |  385|  4.03k|#define LIBUSB_DT_INTERFACE_SIZE		9
  ------------------
  |  Branch (198:14): [True: 9, False: 4.02k]
  ------------------
  199|      9|			usbi_err(ctx, "invalid interface bLength (%u)",
  ------------------
  |  |  339|      9|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      9|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  200|      9|				 bLength);
  201|      9|			r = LIBUSB_ERROR_IO;
  202|      9|			goto err;
  203|  4.02k|		} else if (bLength > size) {
  ------------------
  |  Branch (203:14): [True: 3, False: 4.01k]
  ------------------
  204|      3|			usbi_warn(ctx, "short intf descriptor read %d/%u",
  ------------------
  |  |  340|      3|#define usbi_warn(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      3|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  205|      3|				 size, bLength);
  206|      3|			return parsed;
  207|  4.01k|		} else if (bNumEndpoints > USB_MAXENDPOINTS) {
  ------------------
  |  |  158|  4.01k|#define USB_MAXENDPOINTS	32
  ------------------
  |  Branch (207:14): [True: 13, False: 4.00k]
  ------------------
  208|     13|			usbi_err(ctx, "too many endpoints (%u)", bNumEndpoints);
  ------------------
  |  |  339|     13|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     13|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  209|     13|			r = LIBUSB_ERROR_IO;
  210|     13|			goto err;
  211|     13|		}
  212|       |
  213|  4.00k|		struct libusb_interface_descriptor *altsetting;
  214|  4.00k|		altsetting = (struct libusb_interface_descriptor *)realloc((void *)usb_interface->altsetting,
  215|  4.00k|			sizeof(*altsetting) * (size_t)(usb_interface->num_altsetting + 1));
  216|  4.00k|		if (!altsetting) {
  ------------------
  |  Branch (216:7): [True: 0, False: 4.00k]
  ------------------
  217|      0|			r = LIBUSB_ERROR_NO_MEM;
  218|      0|			goto err;
  219|      0|		}
  220|       |
  221|  4.00k|		ifp = altsetting + usb_interface->num_altsetting;
  222|  4.00k|		ifp->bLength = bLength;
  223|  4.00k|		ifp->bDescriptorType = bDescriptorType;
  224|  4.00k|		ifp->bInterfaceNumber = bInterfaceNumber;
  225|  4.00k|		ifp->bAlternateSetting = bAlternateSetting;
  226|  4.00k|		ifp->bNumEndpoints = bNumEndpoints;
  227|  4.00k|		ifp->bInterfaceClass = bInterfaceClass;
  228|  4.00k|		ifp->bInterfaceSubClass = bInterfaceSubClass;
  229|  4.00k|		ifp->bInterfaceProtocol = bInterfaceProtocol;
  230|  4.00k|		ifp->iInterface = iInterface;
  231|  4.00k|		ifp->extra = NULL;
  232|  4.00k|		ifp->extra_length = 0;
  233|  4.00k|		ifp->endpoint = NULL;
  234|       |
  235|  4.00k|		usb_interface->altsetting = altsetting;
  236|  4.00k|		usb_interface->num_altsetting++;
  237|       |
  238|  4.00k|		if (interface_number == -1)
  ------------------
  |  Branch (238:7): [True: 871, False: 3.13k]
  ------------------
  239|    871|			interface_number = ifp->bInterfaceNumber;
  240|       |
  241|       |		/* Skip over the interface */
  242|  4.00k|		buffer += ifp->bLength;
  243|  4.00k|		parsed += ifp->bLength;
  244|  4.00k|		size -= ifp->bLength;
  245|       |
  246|  4.00k|		begin = buffer;
  247|       |
  248|       |		/* Skip over any interface, class or vendor descriptors */
  249|  5.57k|		while (size >= DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  5.57k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (249:10): [True: 5.46k, False: 118]
  ------------------
  250|  5.46k|			header = (const struct usbi_descriptor_header *)buffer;
  251|  5.46k|			if (header->bLength < DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  5.46k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (251:8): [True: 20, False: 5.44k]
  ------------------
  252|     20|				usbi_err(ctx,
  ------------------
  |  |  339|     20|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     20|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  253|     20|					 "invalid extra intf desc len (%u)",
  254|     20|					 header->bLength);
  255|     20|				r = LIBUSB_ERROR_IO;
  256|     20|				goto err;
  257|  5.44k|			} else if (header->bLength > size) {
  ------------------
  |  Branch (257:15): [True: 24, False: 5.41k]
  ------------------
  258|     24|				usbi_warn(ctx,
  ------------------
  |  |  340|     24|#define usbi_warn(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     24|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  259|     24|					  "short extra intf desc read %d/%u",
  260|     24|					  size, header->bLength);
  261|       |				/* Keep the invariant: bNumEndpoints > 0 implies
  262|       |				 * endpoint != NULL. The endpoint array isn't
  263|       |				 * allocated yet on this early return. */
  264|     24|				ifp->bNumEndpoints = 0;
  265|     24|				return parsed;
  266|     24|			}
  267|       |
  268|       |			/* If we find another "proper" descriptor then we're done */
  269|  5.41k|			if (header->bDescriptorType == LIBUSB_DT_INTERFACE ||
  ------------------
  |  Branch (269:8): [True: 2.62k, False: 2.78k]
  ------------------
  270|  2.78k|			    header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
  ------------------
  |  Branch (270:8): [True: 1.19k, False: 1.59k]
  ------------------
  271|  1.59k|			    header->bDescriptorType == LIBUSB_DT_CONFIG ||
  ------------------
  |  Branch (271:8): [True: 17, False: 1.57k]
  ------------------
  272|  1.57k|			    header->bDescriptorType == LIBUSB_DT_DEVICE)
  ------------------
  |  Branch (272:8): [True: 3, False: 1.57k]
  ------------------
  273|  3.84k|				break;
  274|       |
  275|  1.57k|			buffer += header->bLength;
  276|  1.57k|			parsed += header->bLength;
  277|  1.57k|			size -= header->bLength;
  278|  1.57k|		}
  279|       |
  280|       |		/* Copy any unknown descriptors into a storage area for */
  281|       |		/*  drivers to later parse */
  282|  3.96k|		ptrdiff_t len = buffer - begin;
  283|  3.96k|		if (len > 0) {
  ------------------
  |  Branch (283:7): [True: 304, False: 3.65k]
  ------------------
  284|    304|			void *extra = malloc((size_t)len);
  285|       |
  286|    304|			if (!extra) {
  ------------------
  |  Branch (286:8): [True: 0, False: 304]
  ------------------
  287|      0|				r = LIBUSB_ERROR_NO_MEM;
  288|      0|				goto err;
  289|      0|			}
  290|       |
  291|    304|			memcpy(extra, begin, (size_t)len);
  292|    304|			ifp->extra = (const unsigned char *)extra;
  293|    304|			ifp->extra_length = (int)len;
  294|    304|		}
  295|       |
  296|  3.96k|		if (ifp->bNumEndpoints > 0) {
  ------------------
  |  Branch (296:7): [True: 3.15k, False: 811]
  ------------------
  297|  3.15k|			struct libusb_endpoint_descriptor *endpoint;
  298|  3.15k|			uint8_t i;
  299|       |
  300|  3.15k|			endpoint = (struct libusb_endpoint_descriptor *)calloc(ifp->bNumEndpoints, sizeof(*endpoint));
  301|  3.15k|			if (!endpoint) {
  ------------------
  |  Branch (301:8): [True: 0, False: 3.15k]
  ------------------
  302|      0|				r = LIBUSB_ERROR_NO_MEM;
  303|      0|				goto err;
  304|      0|			}
  305|       |
  306|  3.15k|			ifp->endpoint = endpoint;
  307|  5.25k|			for (i = 0; i < ifp->bNumEndpoints; i++) {
  ------------------
  |  Branch (307:16): [True: 4.34k, False: 910]
  ------------------
  308|  4.34k|				r = parse_endpoint(ctx, endpoint + i, buffer, size);
  309|  4.34k|				if (r < 0)
  ------------------
  |  Branch (309:9): [True: 159, False: 4.18k]
  ------------------
  310|    159|					goto err;
  311|  4.18k|				if (r == 0) {
  ------------------
  |  Branch (311:9): [True: 2.08k, False: 2.10k]
  ------------------
  312|  2.08k|					ifp->bNumEndpoints = i;
  313|  2.08k|					break;
  314|  2.08k|				}
  315|       |
  316|  2.10k|				buffer += r;
  317|  2.10k|				parsed += r;
  318|  2.10k|				size -= r;
  319|  2.10k|			}
  320|  3.15k|		}
  321|       |
  322|       |		/* We check to see if it's an alternate to this one */
  323|  3.80k|		if_desc = (const struct usbi_interface_descriptor *)buffer;
  324|  3.80k|		if (size < LIBUSB_DT_INTERFACE_SIZE ||
  ------------------
  |  |  385|  7.60k|#define LIBUSB_DT_INTERFACE_SIZE		9
  ------------------
  |  Branch (324:7): [True: 140, False: 3.66k]
  ------------------
  325|  3.66k|		    if_desc->bDescriptorType != LIBUSB_DT_INTERFACE ||
  ------------------
  |  Branch (325:7): [True: 48, False: 3.61k]
  ------------------
  326|  3.61k|		    if_desc->bInterfaceNumber != interface_number)
  ------------------
  |  Branch (326:7): [True: 471, False: 3.14k]
  ------------------
  327|    659|			return parsed;
  328|  3.80k|	}
  329|       |
  330|    142|	return parsed;
  331|    201|err:
  332|    201|	clear_interface(usb_interface);
  333|    201|	return r;
  334|  1.06k|}
fuzz_descriptor_parsers.c:parse_endpoint:
   56|  4.34k|{
   57|  4.34k|	const struct usbi_descriptor_header *header;
   58|  4.34k|	const uint8_t *begin;
   59|  4.34k|	void *extra;
   60|  4.34k|	int parsed = 0;
   61|       |
   62|  4.34k|	if (size < DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  4.34k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (62:6): [True: 124, False: 4.22k]
  ------------------
   63|    124|		usbi_err(ctx, "short endpoint descriptor read %d/%d",
  ------------------
  |  |  339|    124|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|    124|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
   64|    124|			 size, DESC_HEADER_LENGTH);
   65|    124|		return LIBUSB_ERROR_IO;
   66|    124|	}
   67|       |
   68|  4.22k|	header = (const struct usbi_descriptor_header *)buffer;
   69|  4.22k|	if (header->bDescriptorType != LIBUSB_DT_ENDPOINT) {
  ------------------
  |  Branch (69:6): [True: 2.07k, False: 2.14k]
  ------------------
   70|  2.07k|		usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  ------------------
  |  |  339|  2.07k|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|  2.07k|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
   71|  2.07k|			header->bDescriptorType, LIBUSB_DT_ENDPOINT);
   72|  2.07k|		return parsed;
   73|  2.14k|	} else if (header->bLength < LIBUSB_DT_ENDPOINT_SIZE) {
  ------------------
  |  |  386|  2.14k|#define LIBUSB_DT_ENDPOINT_SIZE			7
  ------------------
  |  Branch (73:13): [True: 20, False: 2.12k]
  ------------------
   74|     20|		usbi_err(ctx, "invalid endpoint bLength (%u)", header->bLength);
  ------------------
  |  |  339|     20|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     20|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
   75|     20|		return LIBUSB_ERROR_IO;
   76|  2.12k|	} else if (header->bLength > size) {
  ------------------
  |  Branch (76:13): [True: 8, False: 2.11k]
  ------------------
   77|      8|		usbi_warn(ctx, "short endpoint descriptor read %d/%u",
  ------------------
  |  |  340|      8|#define usbi_warn(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      8|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
   78|      8|			  size, header->bLength);
   79|      8|		return parsed;
   80|      8|	}
   81|       |
   82|  2.11k|	endpoint->bLength = buffer[0];
   83|  2.11k|	endpoint->bDescriptorType = buffer[1];
   84|  2.11k|	endpoint->bEndpointAddress = buffer[2];
   85|  2.11k|	endpoint->bmAttributes = buffer[3];
   86|  2.11k|	endpoint->wMaxPacketSize = ReadLittleEndian16(&buffer[4]);
   87|  2.11k|	endpoint->bInterval = buffer[6];
   88|  2.11k|	if (header->bLength >= LIBUSB_DT_ENDPOINT_AUDIO_SIZE) {
  ------------------
  |  |  387|  2.11k|#define LIBUSB_DT_ENDPOINT_AUDIO_SIZE		9	/* Audio extension */
  ------------------
  |  Branch (88:6): [True: 227, False: 1.89k]
  ------------------
   89|    227|		endpoint->bRefresh = buffer[7];
   90|    227|		endpoint->bSynchAddress = buffer[8];
   91|    227|	}
   92|       |
   93|  2.11k|	buffer += header->bLength;
   94|  2.11k|	size -= header->bLength;
   95|  2.11k|	parsed += header->bLength;
   96|       |
   97|       |	/* Skip over the rest of the Class Specific or Vendor Specific */
   98|       |	/*  descriptors */
   99|  2.11k|	begin = buffer;
  100|  3.95k|	while (size >= DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  3.95k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (100:9): [True: 3.86k, False: 85]
  ------------------
  101|  3.86k|		header = (const struct usbi_descriptor_header *)buffer;
  102|  3.86k|		if (header->bLength < DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  3.86k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (102:7): [True: 15, False: 3.85k]
  ------------------
  103|     15|			usbi_err(ctx, "invalid extra ep desc len (%u)",
  ------------------
  |  |  339|     15|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     15|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  104|     15|				 header->bLength);
  105|     15|			return LIBUSB_ERROR_IO;
  106|  3.85k|		} else if (header->bLength > size) {
  ------------------
  |  Branch (106:14): [True: 51, False: 3.80k]
  ------------------
  107|     51|			usbi_warn(ctx, "short extra ep desc read %d/%u",
  ------------------
  |  |  340|     51|#define usbi_warn(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     51|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  108|     51|				  size, header->bLength);
  109|     51|			return parsed;
  110|     51|		}
  111|       |
  112|       |		/* If we find another "proper" descriptor then we're done  */
  113|  3.80k|		if (header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
  ------------------
  |  Branch (113:7): [True: 948, False: 2.85k]
  ------------------
  114|  2.85k|		    header->bDescriptorType == LIBUSB_DT_INTERFACE ||
  ------------------
  |  Branch (114:7): [True: 1.00k, False: 1.84k]
  ------------------
  115|  1.84k|		    header->bDescriptorType == LIBUSB_DT_CONFIG ||
  ------------------
  |  Branch (115:7): [True: 7, False: 1.84k]
  ------------------
  116|  1.84k|		    header->bDescriptorType == LIBUSB_DT_DEVICE)
  ------------------
  |  Branch (116:7): [True: 7, False: 1.83k]
  ------------------
  117|  1.96k|			break;
  118|       |
  119|  1.83k|		usbi_dbg(ctx, "skipping descriptor 0x%x", header->bDescriptorType);
  ------------------
  |  |  342|  1.83k|#define usbi_dbg(ctx ,...)      	_usbi_log(ctx, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|  1.83k|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  120|  1.83k|		buffer += header->bLength;
  121|  1.83k|		size -= header->bLength;
  122|  1.83k|		parsed += header->bLength;
  123|  1.83k|	}
  124|       |
  125|       |	/* Copy any unknown descriptors into a storage area for drivers */
  126|       |	/*  to later parse */
  127|  2.05k|	ptrdiff_t len = buffer - begin;
  128|  2.05k|	if (len <= 0)
  ------------------
  |  Branch (128:6): [True: 1.78k, False: 269]
  ------------------
  129|  1.78k|		return parsed;
  130|       |
  131|    269|	extra = malloc((size_t)len);
  132|    269|	if (!extra)
  ------------------
  |  Branch (132:6): [True: 0, False: 269]
  ------------------
  133|      0|		return LIBUSB_ERROR_NO_MEM;
  134|       |
  135|    269|	memcpy(extra, begin, (size_t)len);
  136|    269|	endpoint->extra = (const unsigned char *)extra;
  137|    269|	endpoint->extra_length = (int)len;
  138|       |
  139|    269|	return parsed;
  140|    269|}
fuzz_descriptor_parsers.c:parse_iad_array:
 1394|    607|{
 1395|    607|	uint8_t i;
 1396|    607|	struct usbi_descriptor_header header;
 1397|    607|	int consumed = 0;
 1398|    607|	const uint8_t *buf = buffer;
 1399|    607|	struct libusb_interface_association_descriptor *iad;
 1400|    607|	int iad_length = 0;
 1401|       |
 1402|    607|	iad_array->iad = NULL;
 1403|    607|	iad_array->length = 0;
 1404|       |
 1405|    607|	if (size < LIBUSB_DT_CONFIG_SIZE) {
  ------------------
  |  |  384|    607|#define LIBUSB_DT_CONFIG_SIZE			9
  ------------------
  |  Branch (1405:6): [True: 0, False: 607]
  ------------------
 1406|      0|		usbi_err(ctx, "short config descriptor read %d/%d",
  ------------------
  |  |  339|      0|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      0|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
 1407|      0|			 size, LIBUSB_DT_CONFIG_SIZE);
 1408|      0|		return LIBUSB_ERROR_IO;
 1409|      0|	}
 1410|       |
 1411|       |	/* First pass: Iterate through desc list, count number of IADs */
 1412|  31.7k|	while (size - consumed >= DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  31.7k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (1412:9): [True: 31.3k, False: 397]
  ------------------
 1413|  31.3k|		header.bLength = buf[0];
 1414|  31.3k|		header.bDescriptorType = buf[1];
 1415|  31.3k|		if (header.bLength < DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  31.3k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (1415:7): [True: 75, False: 31.2k]
  ------------------
 1416|     75|			usbi_err(ctx, "invalid descriptor bLength %d",
  ------------------
  |  |  339|     75|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|     75|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
 1417|     75|				 header.bLength);
 1418|     75|			return LIBUSB_ERROR_IO;
 1419|     75|		}
 1420|  31.2k|		else if (header.bLength > size - consumed) {
  ------------------
  |  Branch (1420:12): [True: 135, False: 31.1k]
  ------------------
 1421|    135|			usbi_warn(ctx, "short config descriptor read %d/%u",
  ------------------
  |  |  340|    135|#define usbi_warn(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|    135|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
 1422|    135|					  size - consumed, header.bLength);
 1423|    135|			return LIBUSB_ERROR_IO;
 1424|    135|		}
 1425|  31.1k|		if (header.bDescriptorType == LIBUSB_DT_INTERFACE_ASSOCIATION)
  ------------------
  |  Branch (1425:7): [True: 20.6k, False: 10.4k]
  ------------------
 1426|  20.6k|			iad_length++;
 1427|  31.1k|		buf += header.bLength;
 1428|  31.1k|		consumed += header.bLength;
 1429|  31.1k|	}
 1430|       |
 1431|    397|	if (iad_length > 0) {
  ------------------
  |  Branch (1431:6): [True: 95, False: 302]
  ------------------
 1432|     95|		iad = (struct libusb_interface_association_descriptor *)calloc((size_t)iad_length, sizeof(*iad));
 1433|     95|		if (!iad)
  ------------------
  |  Branch (1433:7): [True: 0, False: 95]
  ------------------
 1434|      0|			return LIBUSB_ERROR_NO_MEM;
 1435|       |
 1436|     95|		iad_array->iad = iad;
 1437|     95|		iad_array->length = iad_length;
 1438|       |
 1439|       |		/* Second pass: Iterate through desc list, fill IAD structures */
 1440|     95|		i = 0;
 1441|  20.3k|		do {
 1442|  20.3k|			header.bLength = buffer[0];
 1443|  20.3k|			header.bDescriptorType = buffer[1];
 1444|  20.3k|			if (header.bDescriptorType == LIBUSB_DT_INTERFACE_ASSOCIATION && (size >= LIBUSB_DT_INTERFACE_ASSOCIATION_SIZE)) {
  ------------------
  |  |  392|  18.8k|#define LIBUSB_DT_INTERFACE_ASSOCIATION_SIZE	8
  ------------------
  |  Branch (1444:8): [True: 18.8k, False: 1.51k]
  |  Branch (1444:69): [True: 18.6k, False: 135]
  ------------------
 1445|  18.6k|				iad[i].bLength = buffer[0];
 1446|  18.6k|				iad[i].bDescriptorType = buffer[1];
 1447|  18.6k|				iad[i].bFirstInterface = buffer[2];
 1448|  18.6k|				iad[i].bInterfaceCount = buffer[3];
 1449|  18.6k|				iad[i].bFunctionClass = buffer[4];
 1450|  18.6k|				iad[i].bFunctionSubClass = buffer[5];
 1451|  18.6k|				iad[i].bFunctionProtocol = buffer[6];
 1452|  18.6k|				iad[i].iFunction = buffer[7];
 1453|  18.6k|				i++;
 1454|  18.6k|			}
 1455|       |
 1456|  20.3k|			if (size - header.bLength < DESC_HEADER_LENGTH) {
  ------------------
  |  |   28|  20.3k|#define DESC_HEADER_LENGTH	2
  ------------------
  |  Branch (1456:8): [True: 95, False: 20.2k]
  ------------------
 1457|     95|				break;
 1458|     95|			}
 1459|       |
 1460|  20.2k|			size -= header.bLength;
 1461|  20.2k|			buffer += header.bLength;
 1462|  20.2k|		} while (1);
  ------------------
  |  Branch (1462:12): [True: 20.2k, Folded]
  ------------------
 1463|     95|	}
 1464|       |
 1465|    397|	return LIBUSB_SUCCESS;
 1466|    397|}

usbi_log:
   35|  5.50k|              const char *function, const char *format, ...) {
   36|  5.50k|    (void)ctx; (void)level; (void)function; (void)format;
   37|  5.50k|}
LLVMFuzzerTestOneInput:
   53|    630|int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   54|       |    /* The limit of 8192 is comfortably above practical cases and
   55|       |     * still keeping libFuzzer iterations fast and the corpus small */
   56|    630|    if (size < LIBUSB_DT_CONFIG_SIZE || size > 8192)
  ------------------
  |  |  384|  1.26k|#define LIBUSB_DT_CONFIG_SIZE			9
  ------------------
  |  Branch (56:9): [True: 5, False: 625]
  |  Branch (56:41): [True: 18, False: 607]
  ------------------
   57|     23|        return 0;
   58|       |
   59|       |    /* Every call gets a fresh exact-size copy so any byte read past the
   60|       |     * end is caught by ASan. The fuzzer must not observe state between
   61|       |     * iterations. */
   62|    607|    uint8_t *buf;
   63|       |
   64|       |    /* (1) parse_configuration -> parse_interface -> parse_endpoint */
   65|    607|    buf = malloc(size);
   66|    607|    if (!buf) return 0;
  ------------------
  |  Branch (66:9): [True: 0, False: 607]
  ------------------
   67|    607|    memcpy(buf, data, size);
   68|    607|    {
   69|    607|        struct libusb_config_descriptor cfg;
   70|    607|        memset(&cfg, 0, sizeof(cfg));
   71|    607|        if (parse_configuration(NULL, &cfg, buf, (int)size) >= 0)
  ------------------
  |  Branch (71:13): [True: 305, False: 302]
  ------------------
   72|    305|            clear_configuration(&cfg);
   73|    607|    }
   74|    607|    free(buf);
   75|       |
   76|       |    /* (2) parse_iad_array */
   77|    607|    buf = malloc(size);
   78|    607|    if (!buf) return 0;
  ------------------
  |  Branch (78:9): [True: 0, False: 607]
  ------------------
   79|    607|    memcpy(buf, data, size);
   80|    607|    {
   81|    607|        struct libusb_interface_association_descriptor_array iad;
   82|    607|        memset(&iad, 0, sizeof(iad));
   83|    607|        if (parse_iad_array(NULL, &iad, buf, (int)size) == LIBUSB_SUCCESS)
  ------------------
  |  Branch (83:13): [True: 397, False: 210]
  ------------------
   84|    397|            free((void *)iad.iad);
   85|    607|    }
   86|    607|    free(buf);
   87|       |
   88|    607|    return 0;
   89|    607|}

