]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cvar.c
cvar: Don't memset cvar->aliases before it's even allocated
[xonotic/darkplaces.git] / cvar.c
diff --git a/cvar.c b/cvar.c
index 22e8f51258f87a08b356f785e8db80cbadf4fc8a..58562d6b642742e911d879ddc0fdc55dd50af0c4 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);
 
                
 }
@@ -365,6 +366,18 @@ void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
                Cvar_UpdateAutoCvar(var);
 }
 
+void Cvar_Callback(cvar_t *var)
+{
+       if (var == NULL)
+       {
+               Con_Print("Cvar_Callback: var == NULL\n");
+               return;
+       }
+
+       if(var->callback)
+               var->callback(var);
+}
+
 /*
 ============
 Cvar_Set
@@ -413,8 +426,7 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
        Cvar_UpdateAutoCvar(var);
 
        // Call the function stored in the cvar for bounds checking, cleanup, etc
-       if (var->callback)
-               var->callback(var);
+       Cvar_Callback(var);
 }
 
 void Cvar_SetQuick (cvar_t *var, const char *value)
@@ -443,14 +455,6 @@ void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
        Cvar_SetQuick(var, value);
 }
 
-void Cvar_Set_NoCallback(cvar_t *var, const char *value)
-{
-       void (*callback_save)(cvar_t *) = var->callback;
-       var->callback = NULL;
-       Cvar_SetQuick_Internal(var, value);
-       var->callback = callback_save;
-}
-
 /*
 ============
 Cvar_SetValue
@@ -480,6 +484,11 @@ void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
 
 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
 {
+       if (variable == NULL)
+       {
+               Con_Print("Cvar_RegisterCallback: var == NULL\n");
+               return;
+       }
        variable->callback = callback;
 }
 
@@ -489,11 +498,36 @@ void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
        cvar_hash_t *hash;
        int hashindex;
 
-       variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
+       if(!*alias)
+       {
+               Con_Printf("Cvar_RegisterAlias: invalid alias name\n");
+               return;
+       }
+
+       // check for overlap with a command
+       if (Cmd_Exists(cmd_local, alias))
+       {
+               Con_Printf("Cvar_RegisterAlias: %s is a command\n", alias);
+               return;
+       }
+
+       if(Cvar_FindVar(&cvars_all, alias, 0))
+       {
+               Con_Printf("Cvar_RegisterAlias: %s is a cvar\n", alias);
+               return;
+       }
+
+       if(!variable->aliases)
+               variable->aliases = (char **)Z_Malloc(sizeof(char *) * 2); // For NULL terminator
+       else
+               variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliases_size + 1));
+       
+       variable->aliases[variable->aliases_size + 1] = NULL;
+       
        // Add to it
-       variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
-       memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
-       variable->aliasindex++;
+       variable->aliases[variable->aliases_size] = (char *)Z_Malloc(strlen(alias) + 1);
+       memcpy(variable->aliases[variable->aliases_size], alias, strlen(alias) + 1);
+       variable->aliases_size++;
 
        // link to head of list in this hash table index
        hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
@@ -546,8 +580,6 @@ void Cvar_RegisterVariable (cvar_t *variable)
 {
        cvar_state_t *cvars = NULL;
        cvar_t *current, *cvar;
-       char *oldstr;
-       size_t alloclen;
        int i;
 
        switch (variable->flags & (CF_CLIENT | CF_SERVER))
@@ -615,22 +647,21 @@ 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;
        }
 
        // copy the value off, because future sets will Z_Free it
-       oldstr = (char *)variable->string;
-       alloclen = strlen(variable->string) + 1;
-       variable->string = (char *)Z_Malloc (alloclen);
-       memcpy ((char *)variable->string, oldstr, alloclen);
-       variable->defstring = (char *)Z_Malloc (alloclen);
-       memcpy ((char *)variable->defstring, oldstr, alloclen);
+       variable->name = (char *)Mem_strdup(zonemempool, variable->name);
+       variable->string = (char *)Mem_strdup(zonemempool, variable->string);
+       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.
        for (i = 0;i < PRVM_PROG_MAX;i++)
@@ -681,7 +712,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;
@@ -697,8 +728,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 **));
-       memset(cvar->aliases, 0, sizeof(char *));
+       cvar->aliases = NULL;
+       cvar->aliases_size = 0;
+       cvar->initstate = NULL;
 
        if(newdescription && *newdescription)
                cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
@@ -798,13 +830,8 @@ void Cvar_SaveInitState(cvar_state_t *cvars)
        cvar_t *c;
        for (c = cvars->vars;c;c = c->next)
        {
-               c->initstate = true;
-               c->initflags = c->flags;
-               c->initdefstring = Mem_strdup(zonemempool, c->defstring);
-               c->initstring = Mem_strdup(zonemempool, c->string);
-               c->initvalue = c->value;
-               c->initinteger = c->integer;
-               VectorCopy(c->vector, c->initvector);
+               c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
+               memcpy(c->initstate, c, sizeof(cvar_t));
        }
 }
 
@@ -818,22 +845,22 @@ void Cvar_RestoreInitState(cvar_state_t *cvars)
                if (c->initstate)
                {
                        // restore this cvar, it existed at init
-                       if (((c->flags ^ c->initflags) & CF_MAXFLAGSVAL)
-                        || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "")
-                        || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : ""))
+                       if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
+                        || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
+                        || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
                        {
                                Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
                                if (c->defstring)
                                        Z_Free((char *)c->defstring);
-                               c->defstring = Mem_strdup(zonemempool, c->initdefstring);
+                               c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
                                if (c->string)
                                        Z_Free((char *)c->string);
-                               c->string = Mem_strdup(zonemempool, c->initstring);
+                               c->string = Mem_strdup(zonemempool, c->initstate->string);
                        }
-                       c->flags = c->initflags;
-                       c->value = c->initvalue;
-                       c->integer = c->initinteger;
-                       VectorCopy(c->initvector, c->vector);
+                       c->flags = c->initstate->flags;
+                       c->value = c->initstate->value;
+                       c->integer = c->initstate->integer;
+                       VectorCopy(c->initstate->vector, c->vector);
                        cp = &c->next;
                }
                else
@@ -983,11 +1010,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++;
                        }
                }