usbi_log:
 3058|     22|{
 3059|     22|	va_list args;
 3060|       |
 3061|     22|	va_start(args, format);
 3062|     22|	log_v(ctx, level, function, format, args);
 3063|       |	va_end(args);
 3064|     22|}
core.c:get_env_debug_level:
 2579|     22|{
 2580|     22|	enum libusb_log_level level = LIBUSB_LOG_LEVEL_NONE;
 2581|     22|	const char *dbg = getenv("LIBUSB_DEBUG");
 2582|     22|	if (dbg) {
  ------------------
  |  Branch (2582:6): [True: 0, False: 22]
  ------------------
 2583|      0|		long dbg_level = strtol(dbg, NULL, 10);
 2584|      0|		dbg_level = CLAMP(dbg_level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
  ------------------
  |  |   72|      0|	((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
  |  |  ------------------
  |  |  |  Branch (72:3): [True: 0, False: 0]
  |  |  |  Branch (72:28): [True: 0, False: 0]
  |  |  ------------------
  ------------------
 2585|      0|		level = (enum libusb_log_level)dbg_level;
 2586|      0|	}
 2587|     22|	return level;
 2588|     22|}
core.c:log_v:
 2950|     22|{
 2951|     22|	const char *prefix = NULL;
 2952|     22|	char buf[USBI_MAX_LOG_LEN];
 2953|     22|	int global_debug, header_len, text_len;
 2954|     22|	static int has_debug_header_been_displayed = 0;
 2955|       |
 2956|       |#ifdef ENABLE_DEBUG_LOGGING
 2957|       |	global_debug = 1;
 2958|       |	UNUSED(ctx);
 2959|       |#else
 2960|     22|	enum libusb_log_level ctx_level;
 2961|     22|	long default_level_value;
 2962|       |
 2963|     22|	if (ctx) {
  ------------------
  |  Branch (2963:6): [True: 0, False: 22]
  ------------------
 2964|      0|		ctx_level = ctx->debug;
 2965|     22|	} else {
 2966|     22|		default_level_value = usbi_atomic_load(&default_debug_level);
  ------------------
  |  |  128|     22|#define usbi_atomic_load(a)	atomic_load((a))
  ------------------
 2967|     22|		ctx_level = default_level_value < 0 ? get_env_debug_level() : (enum libusb_log_level)default_level_value;
  ------------------
  |  Branch (2967:15): [True: 22, False: 0]
  ------------------
 2968|     22|	}
 2969|       |
 2970|     22|	if (ctx_level < level)
  ------------------
  |  Branch (2970:6): [True: 22, False: 0]
  ------------------
 2971|     22|		return;
 2972|       |
 2973|      0|	global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
 2974|      0|#endif
 2975|       |
 2976|      0|	switch (level) {
 2977|      0|	case LIBUSB_LOG_LEVEL_NONE:	/* Impossible, but keeps compiler happy */
  ------------------
  |  Branch (2977:2): [True: 0, False: 0]
  ------------------
 2978|      0|		assert(0);
  ------------------
  |  Branch (2978:3): [Folded, False: 0]
  |  Branch (2978:3): [Folded, False: 0]
  ------------------
 2979|      0|		return;
 2980|      0|	case LIBUSB_LOG_LEVEL_ERROR:
  ------------------
  |  Branch (2980:2): [True: 0, False: 0]
  ------------------
 2981|      0|		prefix = "error";
 2982|      0|		break;
 2983|      0|	case LIBUSB_LOG_LEVEL_WARNING:
  ------------------
  |  Branch (2983:2): [True: 0, False: 0]
  ------------------
 2984|      0|		prefix = "warning";
 2985|      0|		break;
 2986|      0|	case LIBUSB_LOG_LEVEL_INFO:
  ------------------
  |  Branch (2986:2): [True: 0, False: 0]
  ------------------
 2987|      0|		prefix = "info";
 2988|      0|		break;
 2989|      0|	case LIBUSB_LOG_LEVEL_DEBUG:
  ------------------
  |  Branch (2989:2): [True: 0, False: 0]
  ------------------
 2990|      0|		prefix = "debug";
 2991|      0|		break;
 2992|      0|	default:
  ------------------
  |  Branch (2992:2): [True: 0, False: 0]
  ------------------
 2993|      0|		prefix = "unknown";
 2994|      0|		break;
 2995|      0|	}
 2996|       |
 2997|      0|	if (global_debug) {
  ------------------
  |  Branch (2997:6): [True: 0, False: 0]
  ------------------
 2998|      0|		struct timespec timestamp;
 2999|       |
 3000|      0|		if (!has_debug_header_been_displayed) {
  ------------------
  |  Branch (3000:7): [True: 0, False: 0]
  ------------------
 3001|      0|			has_debug_header_been_displayed = 1;
 3002|      0|			log_str(LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3003|      0|			log_str(LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3004|      0|		}
 3005|       |
 3006|      0|		usbi_get_monotonic_time(&timestamp);
 3007|      0|		TIMESPEC_SUB(&timestamp, &timestamp_origin, &timestamp);
  ------------------
  |  |  299|      0|	do {								\
  |  |  300|      0|		(result)->tv_sec = (a)->tv_sec - (b)->tv_sec;		\
  |  |  301|      0|		(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec;	\
  |  |  302|      0|		if ((result)->tv_nsec < 0L) {				\
  |  |  ------------------
  |  |  |  Branch (302:7): [True: 0, False: 0]
  |  |  ------------------
  |  |  303|      0|			--(result)->tv_sec;				\
  |  |  304|      0|			(result)->tv_nsec += NSEC_PER_SEC;		\
  |  |  ------------------
  |  |  |  |  285|      0|#define NSEC_PER_SEC	1000000000L
  |  |  ------------------
  |  |  305|      0|		}							\
  |  |  306|      0|	} while (0)
  |  |  ------------------
  |  |  |  Branch (306:11): [Folded, False: 0]
  |  |  ------------------
  ------------------
 3008|       |
 3009|      0|		header_len = snprintf(buf, sizeof(buf),
 3010|      0|			"[%2ld.%06ld] [%08lx] libusb: %s [%s] ",
 3011|      0|			(long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000L), usbi_get_tid(), prefix, function);
 3012|      0|	} else {
 3013|      0|		header_len = snprintf(buf, sizeof(buf),
 3014|      0|			"libusb: %s [%s] ", prefix, function);
 3015|      0|	}
 3016|       |
 3017|      0|	if ((header_len < 0) || (header_len >= (int)sizeof(buf)))
  ------------------
  |  Branch (3017:6): [True: 0, False: 0]
  |  Branch (3017:26): [True: 0, False: 0]
  ------------------
 3018|      0|	{
 3019|       |		/* If somehow an error occurred, skip having a header and carry on.
 3020|       |		 * If the buffer was too small and the write was truncated,
 3021|       |		 * remove the header so something useful is output. */
 3022|      0|		header_len = 0;
 3023|      0|	}
 3024|       |
 3025|       |	/* starting right after the end of the header, append the rest, but leave room for the terminator. */
 3026|      0|	int space = (int)sizeof(buf) - header_len - (int)strlen(USBI_LOG_LINE_END);
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3027|      0|	text_len = vsnprintf(buf + header_len, space, format, args);
 3028|       |
 3029|      0|	if (text_len < 0) {
  ------------------
  |  Branch (3029:6): [True: 0, False: 0]
  ------------------
 3030|       |		/* If somehow an error occurred, give up. */
 3031|      0|		return;
 3032|      0|	}
 3033|       |
 3034|      0|	if (text_len >= space) {
  ------------------
  |  Branch (3034:6): [True: 0, False: 0]
  ------------------
 3035|       |		/* The space was too small and the write was truncated.
 3036|       |		 * Add our terminator in the space we left at the end. */
 3037|      0|		(void)snprintf(buf + sizeof(buf) - 1 - strlen(USBI_LOG_LINE_END),
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3038|      0|				 1 + strlen(USBI_LOG_LINE_END),
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3039|      0|				 "%s", USBI_LOG_LINE_END);
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3040|      0|	} else {
 3041|       |		/* Just append the terminator. */
 3042|      0|		(void)snprintf(buf + header_len + text_len,
 3043|      0|				 sizeof(buf) - (size_t)header_len - (size_t)text_len,
 3044|      0|				 "%s", USBI_LOG_LINE_END);
  ------------------
  |  |  169|      0|#define USBI_LOG_LINE_END	"\n"
  ------------------
 3045|      0|	}
 3046|       |
 3047|      0|	log_str(level, buf);
 3048|       |
 3049|       |	/* Per-context log handler */
 3050|      0|#ifndef ENABLE_DEBUG_LOGGING
 3051|      0|	if (ctx && ctx->log_handler)
  ------------------
  |  Branch (3051:6): [True: 0, False: 0]
  |  Branch (3051:13): [True: 0, False: 0]
  ------------------
 3052|      0|		ctx->log_handler(ctx, level, buf);
 3053|      0|#endif
 3054|      0|}

libusb_get_usb_2_0_extension_descriptor:
  958|     52|{
  959|     52|	struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
  960|       |
  961|     52|	if (dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
  ------------------
  |  Branch (961:6): [True: 0, False: 52]
  ------------------
  962|      0|		usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
  ------------------
  |  |  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__)
  |  |  ------------------
  ------------------
  963|      0|			 dev_cap->bDevCapabilityType,
  964|      0|			 LIBUSB_BT_USB_2_0_EXTENSION);
  965|      0|		return LIBUSB_ERROR_INVALID_PARAM;
  966|     52|	} else if (dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
  ------------------
  |  |  395|     52|#define LIBUSB_BT_USB_2_0_EXTENSION_SIZE	7
  ------------------
  |  Branch (966:13): [True: 3, False: 49]
  ------------------
  967|      3|		usbi_err(ctx, "short dev-cap descriptor read %u/%d",
  ------------------
  |  |  339|      3|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      3|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
  968|      3|			 dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
  969|      3|		return LIBUSB_ERROR_IO;
  970|      3|	}
  971|       |
  972|     49|	_usb_2_0_extension = (struct libusb_usb_2_0_extension_descriptor *)malloc(sizeof(*_usb_2_0_extension));
  973|     49|	if (!_usb_2_0_extension)
  ------------------
  |  Branch (973:6): [True: 0, False: 49]
  ------------------
  974|      0|		return LIBUSB_ERROR_NO_MEM;
  975|       |
  976|     49|	_usb_2_0_extension->bLength = dev_cap->bLength;
  977|     49|	_usb_2_0_extension->bDescriptorType = dev_cap->bDescriptorType;
  978|     49|	_usb_2_0_extension->bDevCapabilityType = dev_cap->bDevCapabilityType;
  979|     49|	_usb_2_0_extension->bmAttributes = ReadLittleEndian32(dev_cap->dev_capability_data);
  980|       |
  981|     49|	*usb_2_0_extension = _usb_2_0_extension;
  982|     49|	return LIBUSB_SUCCESS;
  983|     49|}
libusb_free_usb_2_0_extension_descriptor:
  995|     52|{
  996|     52|	free(usb_2_0_extension);
  997|     52|}
libusb_get_ss_usb_device_capability_descriptor:
 1016|     52|{
 1017|     52|	struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
 1018|       |
 1019|     52|	if (dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
  ------------------
  |  Branch (1019:6): [True: 0, False: 52]
  ------------------
 1020|      0|		usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
  ------------------
  |  |  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__)
  |  |  ------------------
  ------------------
 1021|      0|			 dev_cap->bDevCapabilityType,
 1022|      0|			 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
 1023|      0|		return LIBUSB_ERROR_INVALID_PARAM;
 1024|     52|	} else if (dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
  ------------------
  |  |  396|     52|#define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE	10
  ------------------
  |  Branch (1024:13): [True: 6, False: 46]
  ------------------
 1025|      6|		usbi_err(ctx, "short dev-cap descriptor read %u/%d",
  ------------------
  |  |  339|      6|#define usbi_err(ctx, ...)	_usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
  |  |  ------------------
  |  |  |  |  337|      6|#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
  |  |  ------------------
  ------------------
 1026|      6|			 dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
 1027|      6|		return LIBUSB_ERROR_IO;
 1028|      6|	}
 1029|       |
 1030|     46|	_ss_usb_device_cap = (struct libusb_ss_usb_device_capability_descriptor *)malloc(sizeof(*_ss_usb_device_cap));
 1031|     46|	if (!_ss_usb_device_cap)
  ------------------
  |  Branch (1031:6): [True: 0, False: 46]
  ------------------
 1032|      0|		return LIBUSB_ERROR_NO_MEM;
 1033|       |
 1034|     46|	_ss_usb_device_cap->bLength = dev_cap->bLength;
 1035|     46|	_ss_usb_device_cap->bDescriptorType = dev_cap->bDescriptorType;
 1036|     46|	_ss_usb_device_cap->bDevCapabilityType = dev_cap->bDevCapabilityType;
 1037|     46|	_ss_usb_device_cap->bmAttributes = dev_cap->dev_capability_data[0];
 1038|     46|	_ss_usb_device_cap->wSpeedSupported = ReadLittleEndian16(&dev_cap->dev_capability_data[1]);
 1039|     46|	_ss_usb_device_cap->bFunctionalitySupport = dev_cap->dev_capability_data[3];
 1040|     46|	_ss_usb_device_cap->bU1DevExitLat = dev_cap->dev_capability_data[4];
 1041|     46|	_ss_usb_device_cap->bU2DevExitLat = ReadLittleEndian16(&dev_cap->dev_capability_data[5]);
 1042|       |
 1043|     46|	*ss_usb_device_cap = _ss_usb_device_cap;
 1044|     46|	return LIBUSB_SUCCESS;
 1045|     46|}
libusb_free_ss_usb_device_capability_descriptor:
 1190|     52|{
 1191|     52|	free(ss_usb_device_cap);
 1192|     52|}
libusb_get_container_id_descriptor:
 1210|     52|{
 1211|     52|	struct libusb_container_id_descriptor *_container_id;
 1212|       |
 1213|     52|	if (dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
  ------------------
  |  Branch (1213:6): [True: 0, False: 52]
  ------------------
 1214|      0|		usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
  ------------------
  |  |  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__)
  |  |  ------------------
  ------------------
 1215|      0|			 dev_cap->bDevCapabilityType,
 1216|      0|			 LIBUSB_BT_CONTAINER_ID);
 1217|      0|		return LIBUSB_ERROR_INVALID_PARAM;
 1218|     52|	} else if (dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
  ------------------
  |  |  398|     52|#define LIBUSB_BT_CONTAINER_ID_SIZE		20
  ------------------
  |  Branch (1218:13): [True: 13, False: 39]
  ------------------
 1219|     13|		usbi_err(ctx, "short dev-cap descriptor read %u/%d",
  ------------------
  |  |  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__)
  |  |  ------------------
  ------------------
 1220|     13|			 dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
 1221|     13|		return LIBUSB_ERROR_IO;
 1222|     13|	}
 1223|       |
 1224|     39|	_container_id = (struct libusb_container_id_descriptor *)malloc(sizeof(*_container_id));
 1225|     39|	if (!_container_id)
  ------------------
  |  Branch (1225:6): [True: 0, False: 39]
  ------------------
 1226|      0|		return LIBUSB_ERROR_NO_MEM;
 1227|       |
 1228|     39|	_container_id->bLength = dev_cap->bLength;
 1229|     39|	_container_id->bDescriptorType = dev_cap->bDescriptorType;
 1230|     39|	_container_id->bDevCapabilityType = dev_cap->bDevCapabilityType;
 1231|     39|	_container_id->bReserved = dev_cap->dev_capability_data[0];
 1232|     39|	memcpy(_container_id->ContainerID, &dev_cap->dev_capability_data[1], 16);
 1233|       |
 1234|     39|	*container_id = _container_id;
 1235|     39|	return LIBUSB_SUCCESS;
 1236|     39|}
libusb_free_container_id_descriptor:
 1248|     52|{
 1249|     52|	free(container_id);
 1250|     52|}
descriptor.c:ReadLittleEndian16:
   36|     92|{
   37|     92|	return (uint16_t)((uint16_t)p[1] << 8 |
   38|     92|			  (uint16_t)p[0]);
   39|     92|}
descriptor.c:ReadLittleEndian32:
   42|     49|{
   43|     49|	return ((uint32_t)p[3] << 24 |
   44|     49|			(uint32_t)p[2] << 16 |
   45|     49|			(uint32_t)p[1] << 8 |
   46|     49|			(uint32_t)p[0]);
   47|     49|}

LLVMFuzzerTestOneInput:
   17|     52|int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
   18|     52|  if (!data) return 0;
  ------------------
  |  Branch (18:7): [True: 0, False: 52]
  ------------------
   19|       |
   20|       |  /* bLength is 3 (header) + payload; must fit in one byte. */
   21|     52|  uint8_t payload_len = (size > 252) ? 252 : (uint8_t)size;  /* 255 - 3 = 252 */
  ------------------
  |  Branch (21:25): [True: 14, False: 38]
  ------------------
   22|     52|  size_t total_len = 3u + (size_t)payload_len;
   23|       |
   24|       |  /* Allocate header + payload for the flexible array member. */
   25|     52|  struct libusb_bos_dev_capability_descriptor *devcap =
   26|     52|      (struct libusb_bos_dev_capability_descriptor*)
   27|     52|      malloc(sizeof(*devcap) + payload_len);
   28|     52|  if (!devcap) return 0;
  ------------------
  |  Branch (28:7): [True: 0, False: 52]
  ------------------
   29|       |
   30|     52|  devcap->bLength         = (uint8_t)total_len;
   31|     52|  devcap->bDescriptorType = LIBUSB_DT_DEVICE_CAPABILITY; /* 0x10 */
   32|       |  /* Copy fuzz bytes into the variable-length payload. */
   33|     52|  if (payload_len) memcpy(devcap->dev_capability_data, data, payload_len);
  ------------------
  |  Branch (33:7): [True: 52, False: 0]
  ------------------
   34|       |
   35|       |  /* 1) USB 2.0 Extension dev-cap */
   36|     52|  devcap->bDevCapabilityType = LIBUSB_BT_USB_2_0_EXTENSION;
   37|     52|  struct libusb_usb_2_0_extension_descriptor *d20 = NULL;
   38|     52|  (void)libusb_get_usb_2_0_extension_descriptor(NULL, devcap, &d20);
   39|     52|  libusb_free_usb_2_0_extension_descriptor(d20);
   40|       |
   41|       |  /* 2) SuperSpeed USB Device Capability dev-cap */
   42|     52|  devcap->bDevCapabilityType = LIBUSB_BT_SS_USB_DEVICE_CAPABILITY;
   43|     52|  struct libusb_ss_usb_device_capability_descriptor *dss = NULL;
   44|     52|  (void)libusb_get_ss_usb_device_capability_descriptor(NULL, devcap, &dss);
   45|     52|  libusb_free_ss_usb_device_capability_descriptor(dss);
   46|       |
   47|       |  /* 3) Container ID dev-cap */
   48|     52|  devcap->bDevCapabilityType = LIBUSB_BT_CONTAINER_ID;
   49|     52|  struct libusb_container_id_descriptor *dcid = NULL;
   50|     52|  (void)libusb_get_container_id_descriptor(NULL, devcap, &dcid);
   51|     52|  libusb_free_container_id_descriptor(dcid);
   52|       |
   53|     52|  free(devcap);
   54|     52|  return 0;
   55|     52|}

