]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cvar.c
Fix a bug in Cvar_RegisterVirtual which was corrupting memory when adding a second...
[xonotic/darkplaces.git] / cvar.c
diff --git a/cvar.c b/cvar.c
index b4f18c6b30e9104c107a0a9cd585a4cf12f63754..7936c92356be359c13748eca7afbec3250df8ac2 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -1,5 +1,6 @@
 /*
 Copyright (C) 1996-1997 Id Software, Inc.
+Copyright (C) 2000-2021 DarkPlaces contributors
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -43,8 +44,8 @@ cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags)
                if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
                        return hash->cvar;
                else
-                       for (int i = 0; i < hash->cvar->aliasindex; i++)
-                               if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
+                       for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
+                               if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
                                        return hash->cvar;
        return NULL;
 }
@@ -88,8 +89,8 @@ static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_
                if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
                        goto match;
                else
-                       for (int i = 0; i < hash->cvar->aliasindex; i++)
-                               if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
+                       for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
+                               if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
                                        goto match;
                if(parent) *parent = hash->cvar;
        }
@@ -226,8 +227,8 @@ int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int nee
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        h++;
                else
-                       for(int i = 0; i < cvar->aliasindex; i++)
-                               if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
+                       for (char **alias = cvar->aliases; alias && *alias; alias++)
+                               if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
                                        h++;
                
        return h;
@@ -257,9 +258,9 @@ const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, in
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        buf[bpos++] = cvar->name;
                else
-                       for(int i = 0; i < cvar->aliasindex; i++)
-                               if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
-                                       buf[bpos++] = cvar->aliases[i];
+                       for (char **alias = cvar->aliases; alias && *alias; alias++)
+                               if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
+                                       buf[bpos++] = *alias;
                
 
        buf[bpos] = NULL;
@@ -291,9 +292,9 @@ void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int needed
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        Cvar_PrintHelp(cvar, cvar->name, true);
                else
-                       for (int i = 0; i < cvar->aliasindex; i++)
-                               if (!strncasecmp (partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
-                                       Cvar_PrintHelp(cvar, cvar->aliases[i], true);
+                       for (char **alias = cvar->aliases; alias && *alias; alias++)
+                               if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
+                                       Cvar_PrintHelp(cvar, *alias, true);
 
                
 }
@@ -491,21 +492,41 @@ void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
        variable->callback = callback;
 }
 
-void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
+void Cvar_RegisterVirtual(cvar_t *variable, const char *name )
 {
        cvar_state_t *cvars = &cvars_all;
        cvar_hash_t *hash;
        int hashindex;
 
-       variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
-       // Add to it
-       variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
-       memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
-       variable->aliasindex++;
+       if(!*name)
+       {
+               Con_Printf("Cvar_RegisterVirtual: invalid virtual cvar name\n");
+               return;
+       }
+
+       // check for overlap with a command
+       if (Cmd_Exists(cmd_local, name))
+       {
+               Con_Printf("Cvar_RegisterVirtual: %s is a command\n", name);
+               return;
+       }
+
+       if(Cvar_FindVar(&cvars_all, name, 0))
+       {
+               Con_Printf("Cvar_RegisterVirtual: %s is a cvar\n", name);
+               return;
+       }
+
+       // Resize the variable->aliases list to have room for another entry and a null terminator.
+       // This zero-pads when resizing, so we don't need to write the NULL terminator manually here.
+       // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this.
+       variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2));
+       // Add the new alias, and increment the number of aliases in the list
+       variable->aliases[variable->aliases_size++] = (char *)Z_strdup(name);
 
        // link to head of list in this hash table index
        hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
-       hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE;
+       hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE;
        hash->next = cvars->hashtable[hashindex];
        cvars->hashtable[hashindex] = hash;
        hash->cvar = variable;
@@ -621,7 +642,7 @@ void Cvar_RegisterVariable (cvar_t *variable)
        }
 
        // check for overlap with a command
-       if (Cmd_Exists(cmd_client, variable->name) || Cmd_Exists(cmd_server, variable->name))
+       if (Cmd_Exists(cmd_local, variable->name))
        {
                Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
                return;
@@ -633,7 +654,8 @@ void Cvar_RegisterVariable (cvar_t *variable)
        variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
        variable->value = atof (variable->string);
        variable->integer = (int) variable->value;
-       variable->aliasindex = 0;
+       variable->aliases = NULL;
+       variable->aliases_size = 0;
        variable->initstate = NULL;
 
        // Mark it as not an autocvar.
@@ -685,7 +707,7 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f
        }
 
        // check for overlap with a command
-       if (Cmd_Exists(cmd_client, name) || Cmd_Exists(cmd_server, name))
+       if (Cmd_Exists(cmd_local, name))
        {
                Con_Printf("Cvar_Get: %s is a command\n", name);
                return NULL;
@@ -701,9 +723,9 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f
        cvar->defstring = (char *)Mem_strdup(zonemempool, value);
        cvar->value = atof (cvar->string);
        cvar->integer = (int) cvar->value;
-       cvar->aliases = (char **)Z_Malloc(sizeof(char **));
+       cvar->aliases = NULL;
+       cvar->aliases_size = 0;
        cvar->initstate = NULL;
-       memset(cvar->aliases, 0, sizeof(char *));
 
        if(newdescription && *newdescription)
                cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
@@ -983,11 +1005,11 @@ void Cvar_List_f(cmd_state_t *cmd)
                        Cvar_PrintHelp(cvar, cvar->name, true);
                        count++;
                }
-               for (int i = 0; i < cvar->aliasindex; i++)
+               for (char **alias = cvar->aliases; alias && *alias; alias++)
                {
-                       if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false))
+                       if (matchpattern_with_separator(*alias, partial, false, "", false))
                        {
-                               Cvar_PrintHelp(cvar, cvar->aliases[i], true);
+                               Cvar_PrintHelp(cvar, *alias, true);
                                count++;
                        }
                }