Commit 7312b93d3202b77d5e00a9f282fa815cb7d994b1

Sam Lantinga 2022-09-22T22:50:28

Fixed crash if a game controller is disconnected while the connect message is in flight

diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index 2dc32d6..8ecdcd1 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -106,14 +106,17 @@ static void UpdateWindowTitle()
         const char *name = SDL_GameControllerName(gamecontroller);
         const char *serial = SDL_GameControllerGetSerial(gamecontroller);
         const char *basetitle = "Game Controller Test: ";
-        const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + (serial ? 3 + SDL_strlen(serial) : 0) + 1;
+        const size_t titlelen = SDL_strlen(basetitle) + (name ? SDL_strlen(name) : 0) + (serial ? 3 + SDL_strlen(serial) : 0) + 1;
         char *title = (char *)SDL_malloc(titlelen);
 
         retval = SDL_FALSE;
         done = SDL_FALSE;
 
         if (title) {
-            SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
+            SDL_strlcpy(title, basetitle, titlelen);
+            if (name) {
+                SDL_strlcat(title, name, titlelen);
+            }
             if (serial) {
                 SDL_strlcat(title, " (", titlelen);
                 SDL_strlcat(title, serial, titlelen);