core: Prevent attempts to recursively hande events
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 10 Sep 2015 09:39:20 +0000 (02:39 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Tue, 29 Sep 2015 04:51:30 +0000 (21:51 -0700)
commit960a6e7506c4d0d59052056ae2ebfffbe8587175
tree372d705f39f0e482a61815bd25b40b34124b88f3
parent4a0eacbc6f4268c29c1c8d13a89aa42f689bb91c
core: Prevent attempts to recursively hande events

Prior to this commit, it was possible to call certain functions from
within a hotplug or transfer completion callback that would in turn
attempt to handle events (e.g. any of the sync transfer APIs). This
is dangerous because certain events may cause the nested calls to
free memory that is currently in use by the previous calls.

This implementation uses thread-local storage to store a key within
the context that is set to a non-NULL value whenever event handling
is occurring. This allows us to detect when dangerous calls are made
from within event handling context. Such calls will return an error
code of LIBUSB_ERROR_BUSY.

Note that this implementation was chosen because of its portability.
It is supported on all platforms that libusb supports.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/hotplug.c
libusb/io.c
libusb/libusbi.h
libusb/os/threads_posix.h
libusb/os/threads_windows.c
libusb/os/threads_windows.h
libusb/sync.c
libusb/version_nano.h