foxygit / doom Log in
commit 5afef298d6adf6bd6ef852c455647f662a24c995
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Tue Apr 1 21:51:18 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Tue Apr 1 21:51:18 2014 -0400

    osx: Use safe string functions for launcher.

    The OS X launcher used a few unsafe string functions; use snprintf()
    or strlcpy,strlcat here - as this is the launcher for OS X we don't
    need to care about portability.
---
 pkg/osx/Execute.m         |  3 +--
 pkg/osx/IWADController.m  | 18 ++++++++----------
 pkg/osx/LauncherManager.m | 10 ++++------
 3 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/pkg/osx/Execute.m b/pkg/osx/Execute.m
index 0dcfbb7c..0dd6c9a3 100644
--- a/pkg/osx/Execute.m
+++ b/pkg/osx/Execute.m
@@ -75,8 +75,7 @@ static void DoExec(const char *executable, const char *iwad, const char *args)
 {
     char *argv[3];

-    argv[0] = malloc(strlen(executable_path) + strlen(executable) + 3);
-    sprintf(argv[0], "%s/%s", executable_path, executable);
+    asprintf(&argv[0], "%s/%s", executable_path, executable);

     if (iwad != NULL || args != NULL)
     {
diff --git a/pkg/osx/IWADController.m b/pkg/osx/IWADController.m
index 02f83238..fb9223c8 100644
--- a/pkg/osx/IWADController.m
+++ b/pkg/osx/IWADController.m
@@ -308,15 +308,15 @@ static NSString *IWADFilenames[NUM_IWAD_TYPES + 1] =
     IWADLocation *iwadList[NUM_IWAD_TYPES];
     NSString *location;
     unsigned int i;
-    unsigned int len;
     BOOL first;
     char *env;
+    size_t env_len;

     [self getIWADList: iwadList];

     // Calculate length of environment string.

-    len = 0;
+    env_len = 0;

     for (i=0; i<NUM_IWAD_TYPES; ++i)
     {
@@ -324,14 +324,14 @@ static NSString *IWADFilenames[NUM_IWAD_TYPES + 1] =

         if (location != nil && [location length] > 0)
         {
-            len += [location length] + 1;
+            env_len += [location length] + 1;
         }
     }

     // Build string.

-    env = malloc(len);
-    strcpy(env, "");
+    env = malloc(env_len);
+    strlcpy(env, "", env_len);

     first = YES;

@@ -343,10 +343,10 @@ static NSString *IWADFilenames[NUM_IWAD_TYPES + 1] =
         {
             if (!first)
             {
-                strcat(env, ":");
+                strlcat(env, ":", env_len);
             }

-            strcat(env, [location UTF8String]);
+            strlcat(env, [location UTF8String], env_len);
             first = NO;
         }
     }
@@ -366,9 +366,7 @@ static NSString *IWADFilenames[NUM_IWAD_TYPES + 1] =

     doomwadpath = [self doomWadPath];

-    env = malloc(strlen(doomwadpath) + 15);
-
-    sprintf(env, "DOOMWADPATH=%s", doomwadpath);
+    asprintf(&env, "DOOMWADPATH=%s", doomwadpath);

     free(doomwadpath);

diff --git a/pkg/osx/LauncherManager.m b/pkg/osx/LauncherManager.m
index dae2fa7c..b041c99f 100644
--- a/pkg/osx/LauncherManager.m
+++ b/pkg/osx/LauncherManager.m
@@ -297,8 +297,7 @@ static NSString *AppendQuotedFilename(NSString *str, NSString *fileName)
     }

     game_name = [self->iwadController getGameName];
-    executable_name = malloc(strlen(PROGRAM_PREFIX) + strlen(game_name) + 1);
-    sprintf(executable_name, "%s%s", PROGRAM_PREFIX, game_name);
+    asprintf(&executable_name, "%s%s", PROGRAM_PREFIX, game_name);

     ExecuteProgram(executable_name, [iwad UTF8String],
                                     [args UTF8String]);
@@ -319,8 +318,7 @@ static NSString *AppendQuotedFilename(NSString *str, NSString *fileName)
     // to configure, based on the game selected in the dropdown.

     game_name = [self->iwadController getGameName];
-    arg = malloc(strlen(game_name) + 8);
-    sprintf(arg, "-game %s", game_name);
+    asprintf(&arg, "-game %s", game_name);

     ExecuteProgram(PROGRAM_PREFIX "setup", NULL, arg);

@@ -355,14 +353,14 @@ static NSString *AppendQuotedFilename(NSString *str, NSString *fileName)

 - (void) openCMDLINE: (id) sender
 {
-    char *game_name;
+    const char *game_name;
     char filename[32];

     // We need to open the appropriate doc file for the currently
     // selected game.

     game_name = [self->iwadController getGameName];
-    sprintf(filename, "CMDLINE-%s", game_name);
+    snprintf(filename, sizeof(filename), "CMDLINE-%s", game_name);

     OpenDocumentation(filename);
 }