From ef3196bedd2dd2727a2944e664e5d72a0dcb37ff Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 11 May 2026 16:47:26 -0700
Subject: [PATCH] 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)
---
src/joystick/hidapi/SDL_hidapijoystick.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 7629b1ac59825..d5c7d5f2291c5 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/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;