SDL: Fixed crash if SDL API functions are called on a disconnected controller on iOS

From f3fd1ffb99e44adde00a1c07acfa67a55e2e689e Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 20 Oct 2021 15:18:03 -0700
Subject: [PATCH] Fixed crash if SDL API functions are called on a disconnected
 controller on iOS

---
 src/joystick/iphoneos/SDL_mfijoystick.m | 32 ++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/src/joystick/iphoneos/SDL_mfijoystick.m b/src/joystick/iphoneos/SDL_mfijoystick.m
index 6563c9141e..d0d1bf58dc 100644
--- a/src/joystick/iphoneos/SDL_mfijoystick.m
+++ b/src/joystick/iphoneos/SDL_mfijoystick.m
@@ -1268,6 +1268,10 @@ -(void)cleanup
 #ifdef ENABLE_MFI_RUMBLE
     SDL_JoystickDeviceItem *device = joystick->hwdata;
 
+    if (device == NULL) {
+        return SDL_SetError("Controller is no longer connected");
+    }
+
     if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
         if (!device->rumble && device->controller && device->controller.haptics) {
             SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller);
@@ -1294,6 +1298,10 @@ -(void)cleanup
 #ifdef ENABLE_MFI_RUMBLE
     SDL_JoystickDeviceItem *device = joystick->hwdata;
 
+    if (device == NULL) {
+        return SDL_SetError("Controller is no longer connected");
+    }
+
     if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
         if (!device->rumble && device->controller && device->controller.haptics) {
             SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller);
@@ -1319,8 +1327,14 @@ -(void)cleanup
 {
 #ifdef ENABLE_MFI_LIGHT
     @autoreleasepool {
+        SDL_JoystickDeviceItem *device = joystick->hwdata;
+
+        if (device == NULL) {
+            return SDL_FALSE;
+        }
+
         if (@available(macos 11.0, iOS 14.0, tvOS 14.0, *)) {
-            GCController *controller = joystick->hwdata->controller;
+            GCController *controller = device->controller;
             GCDeviceLight *light = controller.light;
             if (light) {
                 return SDL_TRUE;
@@ -1337,8 +1351,14 @@ -(void)cleanup
 {
 #ifdef ENABLE_MFI_LIGHT
     @autoreleasepool {
+        SDL_JoystickDeviceItem *device = joystick->hwdata;
+
+        if (device == NULL) {
+            return SDL_SetError("Controller is no longer connected");
+        }
+
         if (@available(macos 11.0, iOS 14.0, tvOS 14.0, *)) {
-            GCController *controller = joystick->hwdata->controller;
+            GCController *controller = device->controller;
             GCDeviceLight *light = controller.light;
             if (light) {
                 light.color = [[GCColor alloc] initWithRed:(float)red / 255.0f
@@ -1364,8 +1384,14 @@ -(void)cleanup
 {
 #ifdef ENABLE_MFI_SENSORS
     @autoreleasepool {
+        SDL_JoystickDeviceItem *device = joystick->hwdata;
+
+        if (device == NULL) {
+            return SDL_SetError("Controller is no longer connected");
+        }
+
         if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
-            GCController *controller = joystick->hwdata->controller;
+            GCController *controller = device->controller;
             GCMotion *motion = controller.motion;
             if (motion) {
                 motion.sensorsActive = enabled ? YES : NO;