Selaa lähdekoodia

Fixed a crash if we get a HID device with no path

This can happen on Linux if udev_device_get_devnode() fails.

(cherry picked from commit bb4eedd67d9d673925a10f49cd30fd5351f14074)
Sam Lantinga 1 päivä sitten
vanhempi
commit
ef3196bedd
1 muutettua tiedostoa jossa 13 lisäystä ja 3 poistoa
  1. 13 3
      src/joystick/hidapi/SDL_hidapijoystick.c

+ 13 - 3
src/joystick/hidapi/SDL_hidapijoystick.c

@@ -897,14 +897,19 @@ static SDL_HIDAPI_Device *HIDAPI_AddDevice(const struct SDL_hid_device_info *inf
     for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
     }
 
+    char *path = SDL_strdup(info->path);
+    if (!path) {
+        SDL_OutOfMemory();
+        return NULL;
+    }
+
     device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
     if (!device) {
+        SDL_free(path);
         return NULL;
     }
     SDL_SetObjectValid(device, SDL_OBJECT_TYPE_HIDAPI_JOYSTICK, true);
-    if (info->path) {
-        device->path = SDL_strdup(info->path);
-    }
+    device->path = path;
     device->seen = true;
     device->vendor_id = info->vendor_id;
     device->product_id = info->product_id;
@@ -1136,6 +1141,11 @@ static void HIDAPI_UpdateDeviceList(void)
         devs = SDL_hid_enumerate(0, 0);
         if (devs) {
             for (info = devs; info; info = info->next) {
+                if (!info->path) {
+                    // We can't open this, ignore it
+                    continue;
+                }
+
                 device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
                 if (device) {
                     device->seen = true;