From 9d01d090c48c74a29b4ef67e0cd204772a2193c3 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 24 Oct 2014 20:29:56 -0400 Subject: Replace strdup() with M_StringDuplicate(). strdup() can theoretically fail and return NULL. This could lead to a crash or undesirable behavior. Add M_StringDuplicate() which does the same thing but exits with an error if a string cannot be allocated. This fixes #456. Thanks to Quasar for the suggestion. --- src/setup/execute.c | 2 +- src/setup/multiplayer.c | 10 +++++----- src/setup/sound.c | 5 +++-- 3 files changed, 9 insertions(+), 8 deletions(-) (limited to 'src/setup') diff --git a/src/setup/execute.c b/src/setup/execute.c index 1fef6e44..bbb23c38 100644 --- a/src/setup/execute.c +++ b/src/setup/execute.c @@ -268,7 +268,7 @@ static char *GetFullExePath(const char *program) if (sep == NULL) { - result = strdup(program); + result = M_StringDuplicate(program); } else { diff --git a/src/setup/multiplayer.c b/src/setup/multiplayer.c index d833a495..f116ffb2 100644 --- a/src/setup/multiplayer.c +++ b/src/setup/multiplayer.c @@ -856,7 +856,7 @@ static void SelectQueryAddress(TXT_UNCAST_ARG(button), // Set address to connect to: free(connect_address); - connect_address = strdup(button->label); + connect_address = M_StringDuplicate(button->label); // Auto-choose IWAD if there is already a player connected. @@ -1044,7 +1044,7 @@ void SetChatMacroDefaults(void) { if (chat_macros[i] == NULL) { - chat_macros[i] = strdup(defaults[i]); + chat_macros[i] = M_StringDuplicate(defaults[i]); } } } @@ -1053,12 +1053,12 @@ void SetPlayerNameDefault(void) { if (net_player_name == NULL) { - net_player_name = strdup(getenv("USER")); + net_player_name = M_StringDuplicate(getenv("USER")); } if (net_player_name == NULL) { - net_player_name = strdup(getenv("USERNAME")); + net_player_name = M_StringDuplicate(getenv("USERNAME")); } // On Windows, environment variables are in OEM codepage @@ -1073,7 +1073,7 @@ void SetPlayerNameDefault(void) if (net_player_name == NULL) { - net_player_name = strdup("player"); + net_player_name = M_StringDuplicate("player"); } } diff --git a/src/setup/sound.c b/src/setup/sound.c index 76fc0564..2b9bfcbb 100644 --- a/src/setup/sound.c +++ b/src/setup/sound.c @@ -20,6 +20,7 @@ #include "textscreen.h" #include "m_config.h" +#include "m_misc.h" #include "mode.h" #include "sound.h" @@ -324,8 +325,8 @@ void BindSoundVariables(void) M_BindVariable("show_talk", &show_talk); } - timidity_cfg_path = strdup(""); - gus_patch_path = strdup(""); + timidity_cfg_path = M_StringDuplicate(""); + gus_patch_path = M_StringDuplicate(""); // Default sound volumes - different games use different values. -- cgit v1.2.3