mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-28 10:14:14 +00:00
Fixed double-free during multi-threaded hidapi access
The error string is not protected by a mutex, and can be set from multiple threads at the same time. Without this change, it can be double-freed. It can still be double-allocated, leading to a memory leak, but at least it won't crash now. Signed-off-by: Sam Lantinga <slouken@libsdl.org>
This commit is contained in:
@@ -255,8 +255,15 @@ static void free_hid_device(hid_device *dev)
|
||||
|
||||
static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR *op)
|
||||
{
|
||||
free(*error_buffer);
|
||||
*error_buffer = NULL;
|
||||
wchar_t *old_string;
|
||||
#ifdef HIDAPI_ATOMIC_SET_POINTER
|
||||
old_string = HIDAPI_ATOMIC_SET_POINTER(error_buffer, NULL);
|
||||
#else
|
||||
old_string = *error_buffer; *error_buffer = NULL;
|
||||
#endif
|
||||
if (old_string) {
|
||||
free(old_string);
|
||||
}
|
||||
|
||||
/* Only clear out error messages if NULL is passed into op */
|
||||
if (!op) {
|
||||
@@ -320,8 +327,15 @@ static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR
|
||||
|
||||
static void register_string_error_to_buffer(wchar_t **error_buffer, const WCHAR *string_error)
|
||||
{
|
||||
free(*error_buffer);
|
||||
*error_buffer = NULL;
|
||||
wchar_t *old_string;
|
||||
#ifdef HIDAPI_ATOMIC_SET_POINTER
|
||||
old_string = HIDAPI_ATOMIC_SET_POINTER(error_buffer, NULL);
|
||||
#else
|
||||
old_string = *error_buffer; *error_buffer = NULL;
|
||||
#endif
|
||||
if (old_string) {
|
||||
free(old_string);
|
||||
}
|
||||
|
||||
if (string_error) {
|
||||
*error_buffer = _wcsdup(string_error);
|
||||
|
||||
Reference in New Issue
Block a user