From 8851f39b84a147bade43917b9cdb9d469bc9983a Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Mon, 3 Dec 2018 12:35:12 -0500
Subject: [PATCH] Use static joyname instead of malloc

---
 src/sdl/i_system.c   | 16 ++++------------
 src/sdl12/i_system.c | 16 ++++------------
 2 files changed, 8 insertions(+), 24 deletions(-)

diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c
index 905bec098..68ebc5e94 100644
--- a/src/sdl/i_system.c
+++ b/src/sdl/i_system.c
@@ -1453,11 +1453,11 @@ INT32 I_NumJoys(void)
 	return numjoy;
 }
 
+static char joyname[255]; // MAX_PATH; joystick name is straight from the driver
+
 const char *I_GetJoyName(INT32 joyindex)
 {
 	const char *tempname = NULL;
-	size_t templen;
-	char *joyname = NULL;
 	joyindex--; //SDL's Joystick System starts at 0, not 1
 	if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
 	{
@@ -1465,11 +1465,7 @@ const char *I_GetJoyName(INT32 joyindex)
 		{
 			tempname = SDL_JoystickNameForIndex(joyindex);
 			if (tempname)
-			{
-				templen = strlen(tempname);
-				joyname = malloc(templen*sizeof(char));
-				strcpy(joyname, tempname);
-			}
+				strncpy(joyname, tempname, 255);
 		}
 		SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 	}
@@ -1477,11 +1473,7 @@ const char *I_GetJoyName(INT32 joyindex)
 	{
 		tempname = SDL_JoystickNameForIndex(joyindex);
 		if (tempname)
-		{
-			templen = strlen(tempname);
-			joyname = malloc(templen*sizeof(char));
-			strcpy(joyname, tempname);
-		}
+			strncpy(joyname, tempname, 255);
 	}
 	return joyname;
 }
diff --git a/src/sdl12/i_system.c b/src/sdl12/i_system.c
index c4dd96f84..10fbc50ee 100644
--- a/src/sdl12/i_system.c
+++ b/src/sdl12/i_system.c
@@ -1575,11 +1575,11 @@ INT32 I_NumJoys(void)
 	return numjoy;
 }
 
+static char joyname[255]; // MAX_PATH; joystick name is straight from the driver
+
 const char *I_GetJoyName(INT32 joyindex)
 {
 	const char *tempname = NULL;
-	size_t templen;
-	char *joyname = NULL;
 	joyindex--; //SDL's Joystick System starts at 0, not 1
 	if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
 	{
@@ -1587,11 +1587,7 @@ const char *I_GetJoyName(INT32 joyindex)
 		{
 			tempname = SDL_JoystickNameForIndex(joyindex);
 			if (tempname)
-			{
-				templen = strlen(tempname);
-				joyname = malloc(templen*sizeof(char));
-				strcpy(joyname, tempname);
-			}
+				strncpy(joyname, tempname, 255);
 		}
 		SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 	}
@@ -1599,11 +1595,7 @@ const char *I_GetJoyName(INT32 joyindex)
 	{
 		tempname = SDL_JoystickNameForIndex(joyindex);
 		if (tempname)
-		{
-			templen = strlen(tempname);
-			joyname = malloc(templen*sizeof(char));
-			strcpy(joyname, tempname);
-		}
+			strncpy(joyname, tempname, 255);
 	}
 	return joyname;
 }
-- 
GitLab