SDL: IGameInputDevice::GetDeviceInfo() can never return a null pointer

From 85ac0381b7a543542cb1164489a9088c0a6a772d Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 17 Feb 2024 19:18:04 -0800
Subject: [PATCH] IGameInputDevice::GetDeviceInfo() can never return a null
 pointer

The IGameInputDevice instance can't be internally instantiated without it, so it's always guaranteed to be present.
---
 src/joystick/gdk/SDL_gameinputjoystick.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/src/joystick/gdk/SDL_gameinputjoystick.c b/src/joystick/gdk/SDL_gameinputjoystick.c
index bc1c01cbf0fc..b0591d069324 100644
--- a/src/joystick/gdk/SDL_gameinputjoystick.c
+++ b/src/joystick/gdk/SDL_gameinputjoystick.c
@@ -76,15 +76,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
     char tmp[4];
     int idx = 0;
 
-    if (!pDevice) {
-        return SDL_SetError("GAMEINPUT_InternalAddOrFind argument pDevice cannot be NULL");
-    }
-
     devinfo = IGameInputDevice_GetDeviceInfo(pDevice);
-    if (!devinfo) {
-        return SDL_SetError("GAMEINPUT_InternalAddOrFind GetDeviceInfo returned NULL");
-    }
-
     if (devinfo->capabilities & GameInputDeviceCapabilityWireless) {
         bus = SDL_HARDWARE_BUS_BLUETOOTH;
     } else {
@@ -102,7 +94,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
         elem = g_GameInputList.devices[idx];
         if (elem && elem->device == pDevice) {
             /* we're already added */
-            return idx;
+            return 0;
         }
     }
 
@@ -123,7 +115,6 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
         SDL_strlcat(elem->path, tmp, SDL_arraysize(tmp));
     }
 
-    g_GameInputList.devices = devicelist;
     IGameInputDevice_AddRef(pDevice);
     elem->device = pDevice;
     elem->name = "GameInput Gamepad";
@@ -133,10 +124,11 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
     elem->device_instance = SDL_GetNextObjectID();
     elem->wireless = (devinfo->capabilities & GameInputDeviceCapabilityWireless);
     elem->supportedRumbleMotors = devinfo->supportedRumbleMotors;
-    g_GameInputList.devices[g_GameInputList.count] = elem;
 
-    /* finally increment the count and return */
-    return g_GameInputList.count++;
+    g_GameInputList.devices = devicelist;
+    g_GameInputList.devices[g_GameInputList.count++] = elem;
+
+    return 0;
 }
 
 static int GAMEINPUT_InternalRemoveByIndex(int idx)
@@ -194,6 +186,11 @@ static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
     int idx = 0;
     GAMEINPUT_InternalDevice *elem = NULL;
 
+    if (!device) {
+        /* This should never happen, but ignore it if it does */
+        return;
+    }
+
     if (currentStatus & GameInputDeviceConnected) {
         GAMEINPUT_InternalAddOrFind(device);
     } else {