linux_usbfs: Don't mark init_count as volatile

There is no reason to mark init_count as volatile, it is protected by the
linux_hotplug_startstop_lock. Removing the volatile marking fixes the
following Coverity warning:

*** CID 62574:  Side effect in assertion  (ASSERT_SIDE_EFFECT)
/libusb/os/linux_usbfs.c: 460 in op_exit()
454     	return r;
455     }
456
457     static void op_exit(void)
458     {
459     	usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
>>>     CID 62574:  Side effect in assertion  (ASSERT_SIDE_EFFECT)
>>>     Argument "init_count" of assert() has a side effect because the variable is volatile.  The containing function might work differently in a non-debug build.
460     	assert(init_count != 0);
461     	if (!--init_count) {
462     		/* tear down event handler */
463     		(void)linux_stop_event_monitor();
464     	}
465     	usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux_usbfs: op_handle_events: Protect against not finding the device-handle

We scan the list of open devices to find the device-handle based on the fd,
add a check to ensure that we've actually found the handle before continuing.

This fixes the following Coverity warning:

*** CID 62575:  Explicit null dereferenced  (FORWARD_NULL)
/libusb/os/linux_usbfs.c: 2594 in op_handle_events()
2588     			hpriv = _device_handle_priv(handle);
2589     			if (hpriv->fd == pollfd->fd)
2590     				break;
2591     		}
2592
2593     		if (pollfd->revents & POLLERR) {
>>>     CID 62575:  Explicit null dereferenced  (FORWARD_NULL)
>>>     Dereferencing null pointer "hpriv".
2594     			usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd);
2595     			usbi_handle_disconnect(handle);
2596     			/* device will still be marked as attached if hotplug
monitor thread
2597     			 * hasn't processed remove event yet */
2598     			usbi_mutex_static_lock(&linux_hotplug_lock);
2599     			if (handle->dev->attached)

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux_udev: Fix a Coverity warning

This really is a false positive, but easy enough to silence:

*** CID 62578:  Failure to restore non-local value  (MISSING_RESTORE)
/libusb/os/linux_udev.c: 64 in linux_udev_start_event_monitor()
58     	int r;
59
60     	assert(udev_ctx == NULL);
61     	udev_ctx = udev_new();
62     	if (!udev_ctx) {
63     		usbi_err(NULL, "could not create udev context");
>>>     CID 62578:  Failure to restore non-local value  (MISSING_RESTORE)
>>>     Value of non-local "udev_ctx" that was verified to be "NULL" is not restored as it was along other paths.
64     		return LIBUSB_ERROR_OTHER;
65     	}
66
67     	udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
68     	if (!udev_monitor) {
69     		usbi_err(NULL, "could not initialize udev monitor");

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb_submit_transfer: Fix possible deadlock

Fix a possible deadlock due to a lock ordering reversal, caught by Coverity:

*** CID 62579:  Thread deadlock  (ORDER_REVERSAL)
/libusb/io.c: 1451 in libusb_submit_transfer()
1445     	r = calculate_timeout(itransfer);
1446     	if (r < 0) {
1447     		r = LIBUSB_ERROR_OTHER;
1448     		goto out;
1449     	}
1450
>>>     CID 62579:  Thread deadlock  (ORDER_REVERSAL)
>>>     Calling "pthread_mutex_lock" acquires lock "libusb_context.flying_transfers_lock" while holding lock "usbi_transfer.lock" (count: 1 / 4).
1451     	usbi_mutex_lock(&ctx->flying_transfers_lock);
1452     	r = add_to_flying_list(itransfer);
1453     	if (r == LIBUSB_SUCCESS) {
1454     		r = usbi_backend->submit_transfer(itransfer);
1455     	}
1456     	if (r != LIBUSB_SUCCESS) {

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb_submit_transfer: Don't reference the device on submission failure

If submission fails libusb_ref_device will never get balanced by an unref
on completion, since there will be no completion.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb 1.0.19-rc2

Signed-off-by: Hans de Goede <hdegoede@redhat.com>