From: Uwe Hermann Date: Thu, 9 Jul 2015 22:59:48 +0000 (+0200) Subject: fx2lafw: Fix segfault wrt libusb_detach_kernel_driver(). X-Git-Tag: libsigrok-0.4.0~464 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=dc2903bbdbc97d4d0968ffc23d475cf21adc8575;p=libsigrok.git fx2lafw: Fix segfault wrt libusb_detach_kernel_driver(). Move the libusb_detach_kernel_driver() call after the code that sets the usb->devhdl pointer, otherwise it'll be NULL and result in a segfault. #0 libusb_kernel_driver_active (dev=0x0, interface_number=0) at libusb/core.c:1711 #1 dev_open (sdi=0x12d99f0) at src/hardware/fx2lafw/api.c:374 [...] Tested on a device with the default Cypress VID/PID and one with the Saleae Logic VID/PID, both works fine. --- diff --git a/src/hardware/fx2lafw/api.c b/src/hardware/fx2lafw/api.c index 724e2dd4..ada4fcc9 100644 --- a/src/hardware/fx2lafw/api.c +++ b/src/hardware/fx2lafw/api.c @@ -370,16 +370,6 @@ static int dev_open(struct sr_dev_inst *sdi) devc = sdi->priv; usb = sdi->conn; - if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { - if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { - if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { - sr_err("Failed to detach kernel driver: %s.", - libusb_error_name(ret)); - return SR_ERR; - } - } - } - /* * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS * milliseconds for the FX2 to renumerate. diff --git a/src/hardware/fx2lafw/protocol.c b/src/hardware/fx2lafw/protocol.c index 11e3504e..b1c20a71 100644 --- a/src/hardware/fx2lafw/protocol.c +++ b/src/hardware/fx2lafw/protocol.c @@ -244,6 +244,16 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di) break; } + if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { + if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { + if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { + sr_err("Failed to detach kernel driver: %s.", + libusb_error_name(ret)); + return SR_ERR; + } + } + } + ret = command_get_fw_version(usb->devhdl, &vi); if (ret != SR_OK) { sr_err("Failed to get firmware version.");