فهرست منبع

Revert "Fixed double-free during multi-threaded hidapi access"

This reverts commit 2b386b6c80b899c5d77cc68856dbab758ae2985f.

This isn't the right approach. Even if the string itself isn't double-freed, it can be returned to the application and then freed while the application is trying to use it. This really needs to be in thread-local storage to be completely safe.

In SDL we already have a global thread-local error string, so I'm going to make an SDL-specific change to handle the error strings safely.
Sam Lantinga 1 سال پیش
والد
کامیت
d51f84a2e1
4فایلهای تغییر یافته به همراه8 افزوده شده و 45 حذف شده
  1. 0 1
      src/hidapi/SDL_hidapi.c
  2. 2 13
      src/hidapi/linux/hid.c
  3. 2 13
      src/hidapi/mac/hid.c
  4. 4 18
      src/hidapi/windows/hid.c

+ 0 - 1
src/hidapi/SDL_hidapi.c

@@ -529,7 +529,6 @@ static void HIDAPI_ShutdownDiscovery(void)
 /* Platform HIDAPI Implementation */
 
 #define HIDAPI_IGNORE_DEVICE(VID, PID)  SDL_HIDAPI_ShouldIgnoreDevice(VID, PID)
-#define HIDAPI_ATOMIC_SET_POINTER(a, v) SDL_AtomicSetPtr((void **)a, v)
 
 struct PLATFORM_hid_device_;
 typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;

+ 2 - 13
src/hidapi/linux/hid.c

@@ -130,19 +130,8 @@ 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)
 {
-	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);
-	}
+	free(*error_str);
+	*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. */

+ 2 - 13
src/hidapi/mac/hid.c

@@ -243,19 +243,8 @@ 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)
 {
-	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);
-	}
+	free(*error_str);
+	*error_str = utf8_to_wchar_t(msg);
 }
 
 /* Similar to register_error_str, but allows passing a format string with va_list args into this function. */

+ 4 - 18
src/hidapi/windows/hid.c

@@ -255,15 +255,8 @@ static void free_hid_device(hid_device *dev)
 
 static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR *op)
 {
-	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);
-	}
+	free(*error_buffer);
+	*error_buffer = NULL;
 
 	/* Only clear out error messages if NULL is passed into op */
 	if (!op) {
@@ -327,15 +320,8 @@ 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)
 {
-	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);
-	}
+	free(*error_buffer);
+	*error_buffer = NULL;
 
 	if (string_error) {
 		*error_buffer = _wcsdup(string_error);