]> de.git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
fix for segfault in map name completion, submitted by div0
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 30 Jan 2006 12:59:27 +0000 (12:59 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 30 Jan 2006 12:59:27 +0000 (12:59 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5924 d7cf8633-e32d-0410-b094-e92efae38249

console.c

index 4f498a95e7b0cb53d791c13048846278a4fcb7f2..6754b08f603eecff116898092ca63e367cd5e2ba 100644 (file)
--- a/console.c
+++ b/console.c
@@ -1006,6 +1006,8 @@ qboolean GetMapList (const char *s, char *completedname, int completednamebuffer
        for(p=o;p<min;p++)
        {
                k = *(t->filenames[0]+5+p);
+               if(k == 0)
+                       goto endcomplete;
                for(i=1;i<t->numfilenames;i++)
                        if(*(t->filenames[i]+5+p) != k)
                                goto endcomplete;
@@ -1014,7 +1016,7 @@ endcomplete:
        if(p > o)
        {
                memset(completedname, 0, completednamebufferlength);
-               memcpy(completedname, (t->filenames[0]+5), p);
+               memcpy(completedname, (t->filenames[0]+5), min(p, completednamebufferlength - 1));
        }
        Z_Free(len);
        FS_FreeSearch(t);
@@ -1073,7 +1075,8 @@ void Con_DisplayList(const char **list)
 */
 void Con_CompleteCommandLine (void)
 {
-       const char *cmd = "", *s;
+       const char *cmd = "";
+       char *s;
        const char **list[3] = {0, 0, 0};
        char s2[512];
        int c, v, a, i, cmd_len, pos, k;
@@ -1104,11 +1107,17 @@ void Con_CompleteCommandLine (void)
                                char t[MAX_QPATH];
                                if (GetMapList(s, t, sizeof(t)))
                                {
-                                       i = (int)(strlen(t) - strlen(s));
-                                       strcpy((char*)s, t);
-                                       if(s2[0])       //add back chars after cursor
-                                               strcpy(&key_lines[edit_line][key_linepos], s2);
-                                       key_linepos += i;
+                                       // first move the cursor
+                                       key_linepos += strlen(t) - strlen(s);
+
+                                       // and now do the actual work
+                                       *s = 0;
+                                       strlcat(key_lines[edit_line], t, MAX_INPUTLINE);
+                                       strlcat(key_lines[edit_line], s2, MAX_INPUTLINE); //add back chars after cursor
+
+                                       // and fix the cursor
+                                       if(key_linepos > (int) strlen(key_lines[edit_line]))
+                                               key_linepos = strlen(key_lines[edit_line]);
                                }
                                return;
                        }