]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Fix null pointer crash when unsetting a cvar.
authorcloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 25 Jun 2020 15:18:15 +0000 (15:18 +0000)
committercloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 25 Jun 2020 15:18:15 +0000 (15:18 +0000)
When fixing up cvars to use the new "hash" table, I got the logic in
Cvar_FindVarLink wrong. That has been fixed.

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12734 d7cf8633-e32d-0410-b094-e92efae38249

cvar.c

diff --git a/cvar.c b/cvar.c
index 905fa758a6f10b1b8817982635c8fca9324e9235..bcd4763fed5822ee5e65ec7da3bca5f30936d876 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -73,7 +73,7 @@ cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int ne
        return var;
 }
 
-static cvar_hash_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_hash_t **parent, cvar_hash_t ***link, cvar_t **prev_alpha, int neededflags)
+static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_t **parent, cvar_t ***link, cvar_t **prev_alpha, int neededflags)
 {
        int hashindex;
        cvar_hash_t *hash;
@@ -82,7 +82,7 @@ static cvar_hash_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name,
        hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
        if(parent) *parent = NULL;
        if(prev_alpha) *prev_alpha = NULL;
-       if(link) *link = &cvars->hashtable[hashindex];
+       if(link) *link = &cvars->hashtable[hashindex]->cvar;
        for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
        {
                if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
@@ -91,19 +91,19 @@ static cvar_hash_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name,
                        for (int i = 0; i < hash->cvar->aliasindex; i++)
                                if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
                                        goto match;
-               if(parent) *parent = hash;
+               if(parent) *parent = hash->cvar;
        }
        return NULL;
 match:
        if(!prev_alpha || hash->cvar == cvars->vars)
-               return hash;
+               return hash->cvar;
 
        *prev_alpha = cvars->vars;
        // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
        // already - should I still insert '*prev_alpha &&' in the loop?
        while((*prev_alpha)->next != hash->cvar)
                *prev_alpha = (*prev_alpha)->next;
-       return hash;
+       return hash->cvar;
 }
 
 /*
@@ -1071,7 +1071,7 @@ void Cvar_Del_f(cmd_state_t *cmd)
        cvar_state_t *cvars = cmd->cvars;
        int neededflags = ~0;
        int i;
-       cvar_hash_t *hash, *parent, **link;
+       cvar_t *parent, **link;
        cvar_t *cvar, *prev;
 
        if(Cmd_Argc(cmd) < 2)
@@ -1081,8 +1081,7 @@ void Cvar_Del_f(cmd_state_t *cmd)
        }
        for(i = 1; i < Cmd_Argc(cmd); ++i)
        {
-               hash = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
-               cvar = hash->cvar;
+               cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
 
                if(!cvar)
                {
@@ -1108,9 +1107,9 @@ void Cvar_Del_f(cmd_state_t *cmd)
                }
 
                if(parent)
-                       parent->next = hash->next;
+                       parent->next = cvar->next;
                else if(link)
-                       *link = hash->next;
+                       *link = cvar->next;
                if(cvar->description != cvar_dummy_description)
                        Z_Free((char *)cvar->description);