SDL icon indicating copy to clipboard operation
SDL copied to clipboard

may be used uninitialized warning from HIDAPI_JoystickOpen

Open sezero opened this issue 1 year ago • 1 comments

src/joystick/hidapi/SDL_hidapijoystick.c: In function 'HIDAPI_JoystickOpen':
src/joystick/hidapi/SDL_hidapijoystick.c:1103: warning: 'joystickID' may be used uninitialized in this function

Initializing joystickID to -1 would silence this. However, looking at HIDAPI_GetDeviceByIndex, HIDAPI_JoystickOpen doesn't handle its possibility of returning NULL either.

sezero avatar Aug 10 '22 07:08 sezero

I suggest something like the following - needs improvement I guess. @slouken?

diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 69e8488..d6506a3 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -1096,18 +1096,23 @@
     HIDAPI_GetDeviceByIndex(device_index, &joystickID);
     return joystickID;
 }
 
 static int
 HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
 {
-    SDL_JoystickID joystickID;
+    SDL_JoystickID joystickID = -1;
     SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index, &joystickID);
     struct joystick_hwdata *hwdata;
 
+    if (!device) {
+        /* set what error here? */
+        return -1;
+    }
+
     hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
     if (!hwdata) {
         return SDL_OutOfMemory();
     }
     hwdata->device = device;
 
     if (!device->driver->OpenJoystick(device, joystick)) {

sezero avatar Aug 10 '22 07:08 sezero

Fixed, thanks!

slouken avatar Aug 10 '22 13:08 slouken