SDL: Fixed crash when the joystick can't be opened

From eb8b5ed3a4d4186f182e6d9434c5da5f6763267f Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 17 Jul 2023 17:35:38 -0700
Subject: [PATCH] Fixed crash when the joystick can't be opened

---
 test/testcontroller.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/test/testcontroller.c b/test/testcontroller.c
index 6ea7c1660f6c..4e05d31fb9c0 100644
--- a/test/testcontroller.c
+++ b/test/testcontroller.c
@@ -295,16 +295,25 @@ static int StandardizeAxisValue(int nValue)
 
 static void RefreshControllerName(void)
 {
+    const char *name = NULL;
+
     SDL_free(controller_name);
     controller_name = NULL;
 
     if (controller) {
         if (controller->gamepad) {
-            controller_name = SDL_strdup(SDL_GetGamepadName(controller->gamepad));
+            name = SDL_GetGamepadName(controller->gamepad);
         } else {
-            controller_name = SDL_strdup(SDL_GetJoystickName(controller->joystick));
+            name = SDL_GetJoystickName(controller->joystick);
         }
     }
+
+    SDL_free(controller_name);
+    if (name) {
+        controller_name = SDL_strdup(name);
+    } else {
+        controller_name = SDL_strdup("");
+    }
 }
 
 static void SetAndFreeGamepadMapping(char *mapping)