diff --git a/src/platforms/hosted/bmp_libusb.c b/src/platforms/hosted/bmp_libusb.c index c94ec164b30..ed8211f7416 100644 --- a/src/platforms/hosted/bmp_libusb.c +++ b/src/platforms/hosted/bmp_libusb.c @@ -147,79 +147,73 @@ probe_info_s *process_ftdi_probe(void) { probe_info_s *probe_list = NULL; DWORD ftdi_dev_count = 0; - char *serial; char *probe_skip = NULL; - char *manufacturer; - char *product; FT_DEVICE_LIST_INFO_NODE *dev_info; - if (FT_CreateDeviceInfoList(&ftdi_dev_count) == FT_OK) { - dev_info = (FT_DEVICE_LIST_INFO_NODE *)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE) * ftdi_dev_count); - if (dev_info != NULL) { - if (FT_GetDeviceInfoList(dev_info, &ftdi_dev_count) == FT_OK) { - bool useSerial = true; + if (FT_CreateDeviceInfoList(&ftdi_dev_count) != FT_OK) + return NULL; + dev_info = (FT_DEVICE_LIST_INFO_NODE *)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE) * ftdi_dev_count); + if (dev_info == NULL) { + DEBUG_WARN("process_ftdi_probe: memory allocation failed\n"); + return NULL; + } + if (FT_GetDeviceInfoList(dev_info, &ftdi_dev_count) != FT_OK) + return NULL; + bool useSerial = true; + // Device list is loaded, iterate over the found probes + for (size_t devIndex = 0; devIndex < ftdi_dev_count; devIndex++) { + const uint16_t vid = (dev_info[devIndex].ID >> 16U) & 0xffffU; + const uint16_t pid = dev_info[devIndex].ID & 0xffffU; + bool add_probe = true; + + char *serial = strdup(dev_info[devIndex].SerialNumber); + char *product = strdup(dev_info[devIndex].Description); + char *manufacturer = strdup("FTDI"); + size_t string_len = strlen(serial); + if (!string_len) { + free((void *)serial); + serial = strdup("---"); // Unknown serial number + } else { + --string_len; + if (serial[string_len] == 'A') { + serial[string_len] = '\0'; // Remove the trailing "A" + // + // If the serial number is valid, save it for later interface skipping test // - // Device list is loaded, iterate over the found probes + if (string_len) + probe_skip = strdup(serial); // Save the fixed serial number so we can skip other interfaces + string_len = strlen(product); + string_len -= 2; // Product has " A" appended + *(product + string_len) = '\0'; // Remove it // - for (size_t devIndex = 0; devIndex < ftdi_dev_count; devIndex++) { - const uint16_t vid = (dev_info[devIndex].ID >> 16U) & 0xffffU; - const uint16_t pid = dev_info[devIndex].ID & 0xffffU; - bool add_probe = true; - - serial = strdup(dev_info[devIndex].SerialNumber); - serial = strdup(dev_info[devIndex].SerialNumber); - product = strdup(dev_info[devIndex].Description); - manufacturer = strdup("FTDI"); - size_t string_len = strlen(serial); - if (!string_len) { - free((void *)serial); - serial = strdup("---"); // Unknown serial number - } else { - string_len -= 1; - if (*(serial + string_len) == 'A') { - *(serial + string_len) = '\0'; // Remove the trailing "A" - // - // If the serial number is valid, save it for later interface skipping test - // - if (string_len) - probe_skip = - strdup(serial); // Save the fixed serial number so we can skip other interfaces - string_len = strlen(product); - string_len -= 2; // Product has " A" appended - *(product + string_len) = '\0'; // Remove it - // - // If we don't have a saved serial number, use the truncated product for probe skip test - // - if (!probe_skip) { - useSerial = false; - probe_skip = strdup(product); - } - } else { - if (probe_skip) { - if (useSerial) { - if (strstr(serial, probe_skip)) - add_probe = false; // Skip this interface - } else if (strstr(product, probe_skip)) - add_probe = false; - } - } - } - if (add_probe) - probe_list = probe_info_add_by_id( - probe_list, BMP_TYPE_FTDI, NULL, vid, pid, manufacturer, product, serial, strdup("---")); - else { - free(serial); - free(product); - free(manufacturer); - } + // If we don't have a saved serial number, use the truncated product for probe skip test + // + if (!probe_skip) { + useSerial = false; + probe_skip = strdup(product); + } + } else { + if (probe_skip) { + if (useSerial) { + if (strstr(serial, probe_skip)) + add_probe = false; // Skip this interface + } else if (strstr(product, probe_skip)) + add_probe = false; } } - if (probe_skip) - free(probe_skip); - free((void *)dev_info); - } else - DEBUG_WARN("process_ftdi_probe: memory allocation failed\n"); + } + if (add_probe) + probe_list = probe_info_add_by_id( + probe_list, BMP_TYPE_FTDI, NULL, vid, pid, manufacturer, product, serial, strdup("---")); + else { + free(serial); + free(product); + free(manufacturer); + } } + if (probe_skip) + free(probe_skip); + free((void *)dev_info); return probe_list; } #endif