From dafca861238c8d17a5d32241a24ee56df81b8fbb Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 12 Nov 2025 22:23:27 -0800
Subject: [PATCH] Cleanup SDL_UDEV_GetProductSerial()
---
src/core/linux/SDL_udev.c | 31 ++++++++++++++--------------
src/core/linux/SDL_udev.h | 2 +-
src/joystick/linux/SDL_sysjoystick.c | 5 +----
3 files changed, 17 insertions(+), 21 deletions(-)
diff --git a/src/core/linux/SDL_udev.c b/src/core/linux/SDL_udev.c
index 1a2ba4df81df9..0647c9275c6c5 100644
--- a/src/core/linux/SDL_udev.c
+++ b/src/core/linux/SDL_udev.c
@@ -36,7 +36,7 @@
#include "SDL_evdev_capabilities.h"
#include "../unix/SDL_poll.h"
-#define SDL_UDEV_FALLBACK_LIBS "libudev.so.1", "libudev.so.0"
+#define SDL_UDEV_FALLBACK_LIBS "libudev.so.1", "libudev.so.0"
static const char *SDL_UDEV_LIBS[] = { SDL_UDEV_FALLBACK_LIBS };
@@ -247,22 +247,19 @@ bool SDL_UDEV_GetProductInfo(const char *device_path, struct input_id *inpid, in
return false;
}
- if (stat(device_path, &statbuf) == -1) {
+ if (stat(device_path, &statbuf) < 0) {
return false;
}
if (S_ISBLK(statbuf.st_mode)) {
type = 'b';
- }
- else if (S_ISCHR(statbuf.st_mode)) {
+ } else if (S_ISCHR(statbuf.st_mode)) {
type = 'c';
- }
- else {
+ } else {
return false;
}
dev = _this->syms.udev_device_new_from_devnum(_this->udev, type, statbuf.st_rdev);
-
if (!dev) {
return false;
}
@@ -302,19 +299,20 @@ bool SDL_UDEV_GetProductInfo(const char *device_path, struct input_id *inpid, in
return true;
}
-bool SDL_UDEV_GetProductSerial(const char *device_path, const char **serial)
+char *SDL_UDEV_GetProductSerial(const char *device_path)
{
struct stat statbuf;
char type;
struct udev_device *dev;
const char *val;
+ char *result = NULL;
if (!_this) {
- return false;
+ return NULL;
}
if (stat(device_path, &statbuf) < 0) {
- return false;
+ return NULL;
}
if (S_ISBLK(statbuf.st_mode)) {
@@ -322,21 +320,22 @@ bool SDL_UDEV_GetProductSerial(const char *device_path, const char **serial)
} else if (S_ISCHR(statbuf.st_mode)) {
type = 'c';
} else {
- return false;
+ return NULL;
}
dev = _this->syms.udev_device_new_from_devnum(_this->udev, type, statbuf.st_rdev);
if (!dev) {
- return false;
+ return NULL;
}
val = _this->syms.udev_device_get_property_value(dev, "ID_SERIAL_SHORT");
- if (val) {
- *serial = val;
- return true;
+ if (val && *val) {
+ result = SDL_strdup(val);
}
- return false;
+ _this->syms.udev_device_unref(dev);
+
+ return result;
}
void SDL_UDEV_UnloadLibrary(void)
diff --git a/src/core/linux/SDL_udev.h b/src/core/linux/SDL_udev.h
index fbe422077642e..35deaaf3453ec 100644
--- a/src/core/linux/SDL_udev.h
+++ b/src/core/linux/SDL_udev.h
@@ -105,7 +105,7 @@ extern bool SDL_UDEV_LoadLibrary(void);
extern void SDL_UDEV_Poll(void);
extern bool SDL_UDEV_Scan(void);
extern bool SDL_UDEV_GetProductInfo(const char *device_path, struct input_id *inpid, int *class, char **driver);
-extern bool SDL_UDEV_GetProductSerial(const char *device_path, const char **serial);
+extern char *SDL_UDEV_GetProductSerial(const char *device_path);
extern bool SDL_UDEV_AddCallback(SDL_UDEV_Callback cb);
extern void SDL_UDEV_DelCallback(SDL_UDEV_Callback cb);
extern const SDL_UDEV_Symbols *SDL_UDEV_GetUdevSyms(void);
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index c94658f6e01e4..e096bfa7e667e 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -1609,10 +1609,7 @@ static bool LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
}
#ifdef SDL_USE_LIBUDEV
- const char *serial = NULL;
- if (SDL_UDEV_GetProductSerial(item->path, &serial)) {
- joystick->serial = SDL_strdup(serial);
- }
+ joystick->serial = SDL_UDEV_GetProductSerial(item->path);
#endif
// mark joystick as fresh and ready