diff options
author | Simon Howard | 2014-11-04 01:00:12 -0500 |
---|---|---|
committer | Simon Howard | 2014-11-04 01:00:12 -0500 |
commit | 66b295d461789f204d19b7181b1a11804a666728 (patch) | |
tree | 084ac6667e87974938c0802b4fa1a598a2eef1ca | |
parent | 176050dcb3b595b8f4e6ab270e44261e766f9528 (diff) | |
download | chocolate-doom-66b295d461789f204d19b7181b1a11804a666728.tar.gz chocolate-doom-66b295d461789f204d19b7181b1a11804a666728.tar.bz2 chocolate-doom-66b295d461789f204d19b7181b1a11804a666728.zip |
setup: Fix bug with strdup() of NULL pointer.
getenv() can return NULL if the environment variable is not set, but
the result of getenv() was always being passed to M_StringDuplicate()
without any check. This could cause crashes on some platforms.
Instead, rework the code into a first stage that gets the player's
name and a second that duplicates it into a mutable form.
This fixes #455.
-rw-r--r-- | src/setup/multiplayer.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/setup/multiplayer.c b/src/setup/multiplayer.c index 8c8fe0c5..8e123b03 100644 --- a/src/setup/multiplayer.c +++ b/src/setup/multiplayer.c @@ -1059,28 +1059,29 @@ void SetPlayerNameDefault(void) { if (net_player_name == NULL) { - net_player_name = M_StringDuplicate(getenv("USER")); + net_player_name = getenv("USER"); } if (net_player_name == NULL) { - net_player_name = M_StringDuplicate(getenv("USERNAME")); + net_player_name = getenv("USERNAME"); } - // On Windows, environment variables are in OEM codepage - // encoding, so convert to UTF8: - -#ifdef _WIN32 - if (net_player_name != NULL) - { - net_player_name = M_OEMToUTF8(net_player_name); - } -#endif - if (net_player_name == NULL) { - net_player_name = M_StringDuplicate("player"); + net_player_name = "player"; } + + // Now strdup() the string so that it's in a mutable form + // that can be freed when the value changes. + +#ifdef _WIN32 + // On Windows, environment variables are in OEM codepage + // encoding, so convert to UTF8: + net_player_name = M_OEMToUTF8(net_player_name); +#else + net_player_name = M_StringDuplicate(net_player_name); +#endif } void MultiplayerConfig(void) |