usb_get_port_path(): fix libusb error checking

When libusb cannot access a device, libusb_get_port_numbers() will return
an error. Check the return code rather than doing invalid pointer
operations (out-of-bound read).

Avoid segfaults at sigrok-cli startup on my setup where some USB devices are
not accessible and also make Valgrind happier.

Signed-off-by: Vincent Palatin <vpalatin@chromium.org>
This commit is contained in:
Vincent Palatin 2014-11-10 08:17:07 -08:00 committed by Uwe Hermann
parent 68ac991dba
commit 2f004b4bc1
1 changed files with 3 additions and 0 deletions

View File

@ -278,6 +278,9 @@ SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len)
n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers));
if (n < 1)
return SR_ERR;
len = snprintf(path, path_len, "usb/%d-%d", len = snprintf(path, path_len, "usb/%d-%d",
libusb_get_bus_number(dev), port_numbers[0]); libusb_get_bus_number(dev), port_numbers[0]);