shithub: choc

Download patch

ref: 6c24da0dad1477fb0585eefac19349dd60f37c3f
parent: eb3a4033c7d6f1d5e042edd5f416bbc257e40975
author: Simon Howard <fraggle@gmail.com>
date: Sat Mar 29 17:58:08 EDT 2014

setup: Eliminate use of unsafe string functions.

Eliminate use of strcpy, strcat, strncpy, and use the new safe
alternatives.

--- a/src/setup/execute.c
+++ b/src/setup/execute.c
@@ -48,6 +48,7 @@
 #include "mode.h"
 #include "m_argv.h"
 #include "m_config.h"
+#include "m_misc.h"
 
 struct execute_context_s
 {
@@ -271,6 +272,7 @@
 {
     char *result;
     char *sep;
+    size_t result_len;
     unsigned int path_len;
 
     sep = strrchr(myargv[0], DIR_SEPARATOR);
@@ -282,13 +284,13 @@
     else
     {
         path_len = sep - myargv[0] + 1;
+        result_len = strlen(program) + path_len + 1;
+        result = malloc(result_len);
 
-        result = malloc(strlen(program) + path_len + 1);
-
-        strncpy(result, myargv[0], path_len);
+        M_StringCopy(result, myargv[0], result_len);
         result[path_len] = '\0';
 
-        strcat(result, program);
+        M_StringConcat(result, program, result_len);
     }
 
     return result;
--- a/src/setup/multiplayer.c
+++ b/src/setup/multiplayer.c
@@ -899,8 +899,8 @@
     char description[47];
 
     sprintf(ping_time_str, "%ims", ping_time);
-    strncpy(description, querydata->description, 46);
-    description[46] = '\0';
+    M_StringCopy(description, querydata->description,
+                 sizeof(description));
 
     TXT_AddWidgets(results_table,
                    TXT_NewLabel(ping_time_str),
--- a/src/setup/txt_joybinput.c
+++ b/src/setup/txt_joybinput.c
@@ -27,6 +27,7 @@
 
 #include "doomkeys.h"
 #include "joystick.h"
+#include "m_misc.h"
 
 #include "txt_joybinput.h"
 #include "txt_gui.h"
@@ -137,7 +138,7 @@
 
     if (*joystick_input->variable < 0)
     {
-        strcpy(buf, "(none)");
+        M_StringCopy(buf, "(none)", sizeof(buf));
     }
     else
     {
--- a/src/setup/txt_keyinput.c
+++ b/src/setup/txt_keyinput.c
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include "doomkeys.h"
+#include "m_misc.h"
 
 #include "txt_keyinput.h"
 #include "txt_gui.h"
@@ -110,7 +111,7 @@
 
     if (*key_input->variable == 0)
     {
-        strcpy(buf, "(none)");
+        M_StringCopy(buf, "(none)", sizeof(buf));
     }
     else
     {
--- a/src/setup/txt_mouseinput.c
+++ b/src/setup/txt_mouseinput.c
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include "doomkeys.h"
+#include "m_misc.h"
 
 #include "txt_mouseinput.h"
 #include "txt_gui.h"
@@ -75,21 +76,21 @@
     mouse_input->widget.h = 1;
 }
 
-static void GetMouseButtonDescription(int button, char *buf)
+static void GetMouseButtonDescription(int button, char *buf, size_t buf_len)
 {
     switch (button)
     {
         case 0:
-            strcpy(buf, "LEFT");
+            M_StringCopy(buf, "LEFT", buf_len);
             break;
         case 1:
-            strcpy(buf, "RIGHT");
+            M_StringCopy(buf, "RIGHT", buf_len);
             break;
         case 2:
-            strcpy(buf, "MID");
+            M_StringCopy(buf, "MID", buf_len);
             break;
         default:
-            sprintf(buf, "BUTTON #%i", button + 1);
+            snprintf(buf, buf_len, "BUTTON #%i", button + 1);
             break;
     }
 }
@@ -102,11 +103,11 @@
 
     if (*mouse_input->variable < 0)
     {
-        strcpy(buf, "(none)");
+        M_StringCopy(buf, "(none)", sizeof(buf));
     }
     else
     {
-        GetMouseButtonDescription(*mouse_input->variable, buf);
+        GetMouseButtonDescription(*mouse_input->variable, buf, sizeof(buf));
     }
 
     TXT_SetWidgetBG(mouse_input);