io: Track device in usbi_transfer 49/290949/1
authorBenjamin Berg <bberg@redhat.com>
Tue, 15 Feb 2022 10:13:41 +0000 (11:13 +0100)
committerMarek Szyprowski <m.szyprowski@samsung.com>
Wed, 5 Apr 2023 09:08:56 +0000 (11:08 +0200)
commit46cad7e87817cf3f40ada224c90df309feab9f7a
treea7e9312ff3f15d51e98de669eb646b822f65e15f
parent7bc9a0a16eff16f6a7f42e5d15021ab3eaf8e7f9
io: Track device in usbi_transfer

transfer->dev_handle currently has the behaviour that it will be unset
if the device is closed. The sync API uses this fact to catch an error
case.

In other cases, transfer->dev_handle will keep its value, which means
that if the transfer lives longer than the device handle, the pointer
becomes invalid.

The transfer does however keep a reference to the device, which owns the
pointer to the context. As such, we can track this reference internal to
the transfer, and it is set while the transfer is in-flight.

With this, switch the logging infrastructure to use itransfer->dev->ctx
while checking that itransfer->dev is non-NULL.

Note that this was a regression caused by 6cae9c6 ("core: update
usbi_dbg to take the context as an argument"), specifically when
resolving the context while freeing a transfer after closing a device.

Note that the transfer will now keep a reference to the device until it
is free'ed. This allows it to use the correct context for logging even
in libusb_free_transfer.

The alternative to all this would be to just explicitly pass NULL to the
log handler in libusb_free_transfer.

Fixes #1038
Closes #1073

[Cherrypick from mainline commit 7cc06ea ("io: Track device in usbi_transfer")]
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: I5fa80503e166ed0aa9cf906985140d80eccdb8dd
libusb/io.c
libusb/libusbi.h
libusb/version_nano.h