pc-stlinkv2: Fix crash with serial given not matching connected devices.

This commit is contained in:
Uwe Bonnes 2019-08-31 09:57:32 +02:00
parent 052b5f830a
commit d994565f2e

View File

@ -683,7 +683,7 @@ void stlink_init(int argc, char **argv)
goto error; goto error;
} }
int i = 0; int i = 0;
bool multiple_devices = false; int nr_stlinks = 0;
while ((dev = devs[i++]) != NULL) { while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc); int r = libusb_get_device_descriptor(dev, &desc);
@ -699,10 +699,6 @@ void stlink_init(int argc, char **argv)
DEBUG("STLINKV1 not supported\n"); DEBUG("STLINKV1 not supported\n");
continue; continue;
} }
if (Stlink.handle) {
libusb_close(Stlink.handle);
multiple_devices = (serial)? false : true;
}
r = libusb_open(dev, &Stlink.handle); r = libusb_open(dev, &Stlink.handle);
if (r == LIBUSB_SUCCESS) { if (r == LIBUSB_SUCCESS) {
uint8_t data[32]; uint8_t data[32];
@ -736,7 +732,6 @@ void stlink_init(int argc, char **argv)
} }
if (serial && (!strncmp(Stlink.serial, serial, strlen(serial)))) if (serial && (!strncmp(Stlink.serial, serial, strlen(serial))))
DEBUG("Found "); DEBUG("Found ");
if (!serial || (!strncmp(Stlink.serial, serial, strlen(serial)))) {
if (desc.idProduct == PRODUCT_ID_STLINKV2) { if (desc.idProduct == PRODUCT_ID_STLINKV2) {
DEBUG("STLINKV20 serial %s\n", Stlink.serial); DEBUG("STLINKV20 serial %s\n", Stlink.serial);
Stlink.ver_hw = 20; Stlink.ver_hw = 20;
@ -760,19 +755,26 @@ void stlink_init(int argc, char **argv)
} else { } else {
DEBUG("Unknown STLINK variant, serial %s\n", Stlink.serial); DEBUG("Unknown STLINK variant, serial %s\n", Stlink.serial);
} }
} nr_stlinks++;
if (serial && (!strncmp(Stlink.serial, serial, strlen(serial)))) if (serial) {
if (!strncmp(Stlink.serial, serial, strlen(serial))) {
break; break;
} else {
libusb_close(Stlink.handle);
Stlink.handle = 0;
}
}
} else { } else {
DEBUG("Open failed %s\n", libusb_strerror(r)); DEBUG("Open failed %s\n", libusb_strerror(r));
} }
} }
} }
if (multiple_devices) {
DEBUG("Multiple Stlinks. Please specify serial number\n");
goto error_1;
}
if (!Stlink.handle) { if (!Stlink.handle) {
if (nr_stlinks && serial)
DEBUG("No Stlink with given serial number %s\n", serial);
else if (nr_stlinks > 1)
DEBUG("Multiple Stlinks. Please specify serial number\n");
else
DEBUG("No Stlink device found!\n"); DEBUG("No Stlink device found!\n");
goto error; goto error;
} }