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:
Sam Lantinga
2023-05-26 20:28:20 -07:00
parent 860e52c99e
commit 2b386b6c80
4 changed files with 45 additions and 8 deletions

View File

@@ -130,8 +130,19 @@ static wchar_t *utf8_to_wchar_t(const char *utf8)
* Use register_error_str(NULL) to free the error message completely. */
static void register_error_str(wchar_t **error_str, const char *msg)
{
free(*error_str);
*error_str = utf8_to_wchar_t(msg);
wchar_t *old_string;
#ifdef HIDAPI_ATOMIC_SET_POINTER
old_string = HIDAPI_ATOMIC_SET_POINTER(error_str, NULL);
#else
old_string = *error_str; *error_str = NULL;
#endif
if (old_string) {
free(old_string);
}
if (msg) {
*error_str = utf8_to_wchar_t(msg);
}
}
/* Semilar to register_error_str, but allows passing a format string with va_list args into this function. */